Operators in Python
Python operators are special symbols that perform operations on variables and values. They are the magic behind calculations, comparisons, and decision-making in the code! From crunching numbers with arithmetic operators to making smart choices with logical operators, these symbols help you control data and logic effortlessly.
Whether you’re building games, automating tasks, or analyzing data, understanding Python operators will supercharge your programming skills! ⚡
They can be categorized as follows:

1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. They allow programmers to handle numerical calculations efficiently.
Operator | Description | Example |
+ | Addition | 5 + 3 # 8 |
– | Subtraction | 5 – 3 # 2 |
* | Multiplication | 5 * 3 # 15 |
/ | Division | 5 / 2 # 2.5 |
// | Floor Division | 5 // 2 # 2 |
% | Modulus (Remainder) | 5 % 2 # 1 |
** | Exponentiation | 5 ** 2 # 25 |
2. Comparison (Relational) Operators
Comparison operators are used to compare two values and return a Boolean result (True or False). They are commonly used in conditional statements, loops, and decision-making structures.
Comparison operators can be used with numbers, strings (based on lexicographic order), and other comparable data types.
For example,
print("apple" > "Apple") # True
(lowercase ‘a’ has a higher ASCII value than uppercase ‘A’)
They also work in combination with logical operators (and, or, not) for complex conditions.
Operator | Description | Example |
== | Equal to | 5 == 3 # False |
!= | Not equal to | 5 != 3 # True |
> | Greater than | 5 > 3 # True |
< | Less than | 5 < 3 # False |
>= | Greater than or equal to | 5 >= 5 # True |
<= | Less than or equal to | 5 <= 3 # False |
3. Logical Operators
Logical operators are used to combine multiple conditions and return a Boolean result (True or False).
The three main logical operators are and, or, and not. These operators are often used in conditional statements and loops to control program flow.
For example,
if age > 18 and has_license:
(ensures both conditions must be True before executing the block of code)
Operator | Description | Example |
and | Returns True only if both statements are True; otherwise, it returns False. | (5 > 3) and (3 > 1) # True |
or | Returns True if at least one of the conditions is True. | (5 > 3) or (3 < 1) # True |
not | Negates a Boolean value, turning True into False and vice versa. (Reverses the result) | not(5 > 3) # False |
4. Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is =, which assigns the right-hand value to the left-hand variable (e.g., x = 10).
Python also provides compound assignment operators that combine arithmetic operations with assignment, such as +=, -=, *=, /=, %=, **=, and //=. These operators help in writing concise and efficient code by reducing redundancy in operations.
Operator | Description | Example |
= | Assign value | x = 5 |
+= | Add and assign | x += 3 # x = x + 3 |
-= | Subtract and assign | x -= 2 # x = x – 2 |
*= | Multiply and assign | x *= 4 # x = x * 4 |
/= | Divide and assign | x /= 2 # x = x / 2 |
//= | Floor divide and assign | x //= 2 # x = x // 2 |
%= | Modulus and assign | x %= 3 # x = x % 3 |
**= | Exponentiate and assign | x **= 2 # x = x ** 2 |
5. Bitwise Operators
Bitwise operators perform operations at the binary level, manipulating individual bits of integers. These operators include,
- AND (&)
The AND (&) operator returns 1 if both corresponding bits are 1, otherwise 0.
- OR (|)
The OR (|) operator returns 1 if at least one bit is 1.
- XOR (^)
The XOR (^) operator returns 1 if the bits are different.
- NOT (~)
The NOT (~) operator inverts all bits.
- Left Shift (<<) and Right Shift (>>)
The Left Shift (<<) moves bits to the left, effectively multiplying by powers of 2, while the Right Shift (>>) moves bits to the right, effectively dividing by powers of 2.
These operators are commonly used in low-level programming, cryptography, and performance optimizations.
Operator | Description | Example | Binary Representation |
& | AND | 5 & 3 # 1 | 5 = 101 3 = 011 ———– 5&3 = 001 (Decimal:1) |
| | OR | 5 | 3 # 7 | 5 = 101 3 = 011 ———– 5 | 3 = 111 (Decimal: 7) |
^ | XOR | 5 ^ 3 # 6 | 5 = 101 3 = 011 ———– 5 ^ 3 = 110 (Decimal: 6) |
~ | NOT | ~5 # -6 | 5 = 00000101, ~5 = 11111010 (-6 in two’s complement) |
<< | Left Shift | 5 << 1 # 10 | 5 = 101 → 1010 (10) |
>> | Right Shift | 5 >> 1 # 2 | 5 = 101 → 10 (2) |
6. Membership Operators
Membership operators are used to check whether a value exists within a sequence, such as a string, list, tuple, dictionary, or set.
These operators are commonly used in loops, conditionals, and searching operations.
Operator | Description | Example |
in | Returns True if the specified value is present in the sequence | ‘a’ in ‘apple’ # True |
not in | Returns True if value is not in sequence | ‘b’ not in ‘apple’ # True |
Note: In dictionaries, membership operators check for the presence of keys, not values (“name” in {“name”: “Alice”, “age”: 25} returns True).
Examples:
Expression | Description | Result |
3 in [1, 2, 3, 4, 5] | Checks if 3 is in the list [1, 2, 3, 4, 5] | True |
10 in [1, 2, 3] | Checks if 10 is in the list [1, 2, 3] | False |
‘name’ in {‘name’: ‘Alice’, ‘age’: 25} | Checks if ‘name’ is a key in the dictionary | True |
‘Alice’ in {‘name’: ‘Alice’, ‘age’: 25} | Checks if ‘Alice’ is a key in the dictionary (not values) | False |
50 not in (10, 20, 30, 40) | Checks if 50 is not in the tuple (10, 20, 30, 40) | True |
‘hello’ not in ‘world’ | Checks if ‘hello’ is not in the string ‘world’ | True |
7. Identity Operators
Identity operators are used to compare the memory addresses of two objects to check if they refer to the same object.
Operator | Description | Example |
is | Returns True if two variables point to the same object in memory. | x is y |
is not | Returns True if they refer to different objects. | x is not y |
Unlike the equality operator (==), which checks for value equality, identity operators check for reference equality.
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is c) # False (different objects)
print(a == c) # True (same value)
Identity operators are useful when dealing with mutable objects like lists and dictionaries.
These operators form the backbone of Python expressions and are essential for programming efficiently. 🚀