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
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow. Hereâs a list
10 min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runt
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples. The below Python section contains a wide collection of Python programming examples. These Python c
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
10 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list(). Let's look at a simple exa
3 min read