Logic gates are the basic building blocks of digital systems. They work by taking one or more binary inputs (0 or 1) and giving a single binary output based on a specific rule of logic. Each gate performs a different function depending on the values it receives. Using Python, we can easily simulate the behavior of these gates through simple code. This makes it easier to understand how digital circuits work and how decisions are made in computing, without needing any physical hardware.
Types of Logic Gates in Python
There are seven basic logic gates in Python. These are the following:
AND Gate
AND gate checks if both things are true. It only gives 1 (true) when both inputs are 1. If even one input is 0, it gives 0.

AND Gate
Example:
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = a & b
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
Explanation: Outer loop goes through the values of a, while the inner loop goes through the values of b. For each pair, it calculates the bitwise AND (a & b), which results in 1 only if both a and b are 1, otherwise the result is 0.
OR Gate
The OR gate checks if at least one thing is true. If any input is 1, it gives 1. It gives 0 only when both inputs are 0.

OR Gates
Example:
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = a | b
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
Explanation: Outer loop iterates through the values of a, while the inner loop iterates through the values of b. For each pair, it calculates the bitwise OR (a | b), which results in 1 if either a or b is 1, otherwise the result is 0.
NOT Gate
NOT gate is different from the others, it takes just one input and flips it. If the input is 1, it becomes 0. If it’s 0, it becomes 1. It’s also called an inverter.

NOT Gate
Example:
Python
print("A | Output")
for a in [0, 1]:
res = 1 if a == 0 else 0
print(f"{a} | {res}")
OutputA | Output
0 | 1
1 | 0
Explanation: Loop iterates through the values of a (0 and 1). For each value of a, it checks if a is 0. If a is 0, the res is set to 1,otherwise it is set to 0.
NAND Gate
NAND gate is the opposite of the AND gate. It gives 0 only when both inputs are 1. In all other cases, it gives 1.

NAND Gate
Example:
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = 0 if a & b else 1
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 1
0 1 | 1
1 0 | 1
1 1 | 0
Explanation: Outer loop iterates through the values of a, while the inner loop iterates through the values of b. For each pair of a and b, it performs the bitwise AND operation (a & b). If the result of the AND operation is 1, the output (res) is set to 0 (since NAND is the opposite of AND). Otherwise, the result is set to 1.
NOR Gate
NOR gate is the opposite of the OR gate. It gives 1 only when both inputs are 0. If even one input is 1, it gives 0.

NOR Gate
Example:
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = 0 if a | b else 1
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 1
0 1 | 0
1 0 | 0
1 1 | 0
Explanation: Outer loop iterates over a and the inner loop over b. For each pair, it performs a bitwise OR (a | b). If the result is 1 (either a or b is 1), res is set to 0. If both are 0, res becomes 1, simulating a NOR gate.
XOR Gate
XOR gate is a little smart, it gives 1 when the inputs are different. If both inputs are the same, it gives 0.

XOR Gate
Example:
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = a ^ b
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
Explanation: Outer loop goes through values of a and the inner loop through b. For each pair, it performs a bitwise XOR (a ^ b). The result is 1 only if a and b are different otherwise, it’s 0.
XNOR
XNOR gate is the opposite of XOR. It gives 1 when the inputs are the same. If the inputs are different, it gives 0.

XNOR Gate
Python
print("A B | Output")
for a in [0, 1]:
for b in [0, 1]:
res = 1 if a == b else 0
print(f"{a} {b} | {res}")
OutputA B | Output
0 0 | 1
0 1 | 0
1 0 | 0
1 1 | 1
Explanation: Outer loop iterates over values of a and the inner loop over b. For each pair, it checks if a and b are equal. If they are the same, res is set to 1 otherwise, it’s 0.
Similar Reads
gentag Library in Python
The gentag library in python provides an efficient way for tagging Python objects. These arbitrary Python objects once assigned with a python tag can further be quarried based on the tags. In this article, the focus is on assignment, manipulations, and operations on tagging using python. Installatio
2 min read
filter() in python
The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. Let's see a simple example of filter() function in python: Example Usage of filter()[GFGTABS] Python # Function to check if a number is even def even(n): return n % 2
3 min read
Any All in Python
Any and All are two built-in functions provided in Python used for successive And/Or. In this article, we will see about any and all in Python. What is Any in PythonAny Returns true if any of the items is True and returns False if empty or all are false. Any can be thought of as a sequence of OR ope
3 min read
eval in Python
Python eval() function parse the expression argument and evaluate it as a Python expression and runs Python expression (code) within the program. Python eval() Function SyntaxSyntax: eval(expression, globals=None, locals=None) Parameters: expression: String is parsed and evaluated as a Python expres
6 min read
Expressions in Python
An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides which operat
5 min read
Python 3 basics
Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the
10 min read
Types of Arguments in Python
Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python . In this example, we will create a simple function in Python to check whether the number passed as an argument to the
3 min read
Python Features
Python is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming. In Python, we don't need to declare the type of variable because it is a dynamically typed language. For example, x = 10 Here,
5 min read
Learn Python Basics
âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read
Python If Else on One Line
In Python, if-else conditions allow us to control the flow of execution based on certain conditions. While traditional if-else statements are usually written across multiple lines, Python offers a more compact and elegant way to express these conditions on a single line. Python if-else in One LineIn
3 min read