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 GatesExample:
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 GateExample:
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 GateExample:
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 GateExample:
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 GateExample:
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
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 eligib
2 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
2 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 expressio
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 i
2 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
Code Golfing in Python Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language,
8 min read