Python Operators Tutorial - Arithmetic, Logical, Comparison
Python Operators
An operators are used to perform a mathematical operation on single or multiple operands. There are various operators available in Python, which are categorized into 5 parts-
Arithmetic Operators:-
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder of a division) |
| ** | Exponentiation |
| // | Floor Division |
Assignment Operators:-
| Operator | Description |
|---|---|
| = | Assign |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Modulus and assign |
| //= | - |
| **= | - |
| &= | - |
| |= | - |
| ^= | - |
| >>= | - |
| <<= | - |
Comparison Operators:-
| Operator | Description |
|---|---|
| Operator | Description |
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| >= | Greater than or equal to |
| < | Less than |
| <= | Less than or equal to |
Boolean or Logical Operators:-
| Operator | Description |
|---|---|
| and | Logical AND |
| or | Logical OR |
| not | Logical NOT |
Identity Operators:-
| Operator | Description |
|---|---|
| is | - |
| is not | - |
Membership Operators:-
| Operator | Description |
|---|---|
| in | - |
| not in | - |
Bitwise Operators:-
| Operator | Description |
|---|---|
| & | - |
| | | - |
| ^ | - |
| ~ | - |
| << | - |
| >> | - |
Python Operators in Action
Let's see all the major operator categories with practical code examples.
a, b = 10, 3
# Arithmetic
print(a + b) # 13 - addition
print(a - b) # 7 - subtraction
print(a * b) # 30 - multiplication
print(a / b) # 3.333... - true division (always float)
print(a // b) # 3 - floor division (integer result)
print(a % b) # 1 - modulus (remainder)
print(a ** b) # 1000 - exponentiation
# Assignment operators
x = 5
x += 3; print(x) # 8
x -= 2; print(x) # 6
x *= 4; print(x) # 24
x //= 5; print(x) # 4
x **= 2; print(x) # 16
x %= 5; print(x) # 1
Comparison, Identity, and Membership Operators
# Comparison operators
print(5 == 5) # True
print(5 != 3) # True
print(10 > 5) # True
print(3 >= 3) # True
print(2 < 5) # True
print(4 <= 3) # False
# Logical operators
print(True and False) # False
print(True or False) # True
print(not True) # False
# Identity operators - check if same object in memory
a = [1, 2, 3]
b = a # same reference
c = [1, 2, 3] # different object, same value
print(a is b) # True - same object
print(a is c) # False - different objects
print(a is not c) # True
# Membership operators - check if value exists in sequence
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits) # True
print('mango' not in fruits) # True
text = 'Hello, Python!'
print('Python' in text) # True
print('Java' not in text) # True
# Walrus operator := (Python 3.8+)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if (n := len(numbers)) > 5:
print(f"List has {n} elements, which is more than 5")
Key Takeaways
- Python uses // for floor division (integer result) and / for true division (always returns float).
- The ** operator handles exponentiation - use it instead of pow() for simple cases.
- is checks object identity (same memory address); == checks value equality.
- in and not in are membership operators - they work on lists, tuples, strings, sets, and dicts.
- Python logical operators are words (and, or, not) not symbols (&& || !) like in C/Java.
- The walrus operator := (Python 3.8+) assigns and returns a value in a single expression.
Level Up Your Python Skills
Master Python with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related Python Topics