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
Gotchas in Python
Python is a go-to language for most of the newcomers into the programming world. This is because it is fairly simple, highly in-demand, and ultimately powerful. But there are some cases which might confuse or rather trick a rookie coder. These are called "Gotchas"! Originating from the informal term
5 min read
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
Access List Items in Python
Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
Python if AND
In Python, if statement is a conditional statement that allows us to run certain code only if a specific condition is true. By combining it with the AND operator, we can check if all conditions are true, giving us more control over the flow of our program. Example : This program checks are you eligi
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
Python If Else in 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
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
Python if OR
In Python, if statement is the conditional statement that allow us to run certain code only if a specific condition is true . By combining it with OR operator, we can check if any one of multiple conditions is true, giving us more control over our program. Example: This program check whether the no
2 min read