Python Operators Complete Guide
Learn all Python operators - arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators with practical examples and operator precedence rules.
Arithmetic
Math operations
Comparison
Relational operators
Logical
AND, OR, NOT
Bitwise
Binary operations
What are Python Operators?
Operators are special symbols in Python that perform operations on variables and values. Python provides various types of operators to perform different tasks.
Key Concept
Operators work on operands. For example, in 5 + 3, + is the operator, and 5 and 3 are operands.
# Arithmetic operators
result = 10 + 5 # Addition
result = 10 - 5 # Subtraction
result = 10 * 5 # Multiplication
result = 10 / 3 # Division (returns float)
# Comparison operators
is_equal = (10 == 5) # Equal to
is_greater = (10 > 5) # Greater than
is_less = (10 < 5) # Less than
# Logical operators
result = (10 > 5) and (3 < 7) # AND operator
result = (10 > 5) or (3 > 7) # OR operator
result = not (10 > 5) # NOT operator
Python Operators Classification
Python operators are classified into 7 main categories. Each type serves a specific purpose in programming.
Complete Operators Reference Table
| Operator Type | Symbol/Name | Description | Example | Result |
|---|---|---|---|---|
| Arithmetic | + | Addition - Adds two operands | 5 + 3 |
8 |
| Arithmetic | - | Subtraction - Subtracts right operand from left | 10 - 4 |
6 |
| Arithmetic | * | Multiplication - Multiplies two operands | 7 * 3 |
21 |
| Arithmetic | / | Division - Divides left operand by right (float result) | 10 / 3 |
3.333... |
| Arithmetic | // | Floor Division - Divides and rounds down to nearest integer | 10 // 3 |
3 |
| Arithmetic | % | Modulus - Returns remainder of division | 10 % 3 |
1 |
| Arithmetic | ** | Exponentiation - Raises left operand to power of right | 2 ** 3 |
8 |
| Comparison | == | Equal to - Returns True if operands are equal | 5 == 5 |
True |
| Comparison | != | Not equal to - Returns True if operands are not equal | 5 != 3 |
True |
| Comparison | > | Greater than - Returns True if left is greater than right | 10 > 5 |
True |
| Comparison | < | Less than - Returns True if left is less than right | 3 < 7 |
True |
| Comparison | >= | Greater than or equal to | 10 >= 10 |
True |
| Comparison | <= | Less than or equal to | 5 <= 10 |
True |
| Logical | and | Logical AND - True if both operands are True | True and False |
False |
| Logical | or | Logical OR - True if at least one operand is True | True or False |
True |
| Logical | not | Logical NOT - Reverses the logical state | not True |
False |
| Assignment | = | Assignment - Assigns right value to left variable | x = 5 |
x becomes 5 |
| Assignment | += | Add AND - Adds right to left and assigns to left | x += 3 (same as x = x + 3) |
x increases by 3 |
| Assignment | -= | Subtract AND | x -= 2 |
x decreases by 2 |
| Assignment | *= | Multiply AND | x *= 4 |
x multiplied by 4 |
| Assignment | /= | Divide AND (float division) | x /= 2 |
x divided by 2 |
| Assignment | //= | Floor Divide AND | x //= 2 |
x floor divided by 2 |
| Assignment | %= | Modulus AND | x %= 3 |
x = x % 3 |
| Bitwise | & | Bitwise AND - Sets each bit to 1 if both bits are 1 | 5 & 3 (101 & 011) |
1 (001) |
| Bitwise | | | Bitwise OR - Sets each bit to 1 if one of bits is 1 | 5 | 3 (101 | 011) |
7 (111) |
| Bitwise | ^ | Bitwise XOR - Sets each bit to 1 if only one bit is 1 | 5 ^ 3 (101 ^ 011) |
6 (110) |
| Bitwise | ~ | Bitwise NOT - Inverts all bits | ~5 |
-6 |
| Bitwise | << | Left shift - Shifts bits left, fills with 0 | 5 << 1 (101 becomes 1010) |
10 |
| Bitwise | >> | Right shift - Shifts bits right | 5 >> 1 (101 becomes 10) |
2 |
| Membership | in | Returns True if value exists in sequence | 'a' in "apple" |
True |
| Membership | not in | Returns True if value doesn't exist in sequence | 'z' not in "apple" |
True |
| Identity | is | Returns True if both variables point to same object | x is y |
True/False |
| Identity | is not | Returns True if variables point to different objects | x is not y |
True/False |
==compares values,iscompares object identityandandorare logical operators,&and|are bitwise operators- Assignment operators (
+=,-=) modify variables in place
Operator Examples with Output
Let's explore practical examples of each operator type with actual Python code and output.
# Arithmetic Operators
a = 10
b = 3
print("Arithmetic Operators:")
print(f"{a} + {b} = {a + b}") # 13
print(f"{a} - {b} = {a - b}") # 7
print(f"{a} * {b} = {a * b}") # 30
print(f"{a} / {b} = {a / b}") # 3.3333...
print(f"{a} // {b} = {a // b}") # 3 (floor division)
print(f"{a} % {b} = {a % b}") # 1 (remainder)
print(f"{a} ** {b} = {a ** b}") # 1000 (10^3)
# Integer division returns float in Python 3
print(f"Type of 10/3: {type(10/3)}") #
# Comparison Operators
x = 10
y = 5
print("\nComparison Operators:")
print(f"{x} == {y}: {x == y}") # False
print(f"{x} != {y}: {x != y}") # True
print(f"{x} > {y}: {x > y}") # True
print(f"{x} < {y}: {x < y}") # False
print(f"{x} >= {y}: {x >= y}") # True
print(f"{x} <= {y}: {x <= y}") # False
# Logical Operators
print("\nLogical Operators:")
print(f"True and False: {True and False}") # False
print(f"True or False: {True or False}") # True
print(f"not True: {not True}") # False
# Combined logical expression
age = 25
has_license = True
can_drive = (age >= 18) and has_license
print(f"\nAge: {age}, Has License: {has_license}")
print(f"Can drive: {can_drive}") # True
# Assignment Operators
x = 10
print("Assignment Operators:")
print(f"Initial x: {x}")
x += 5 # x = x + 5
print(f"After x += 5: {x}") # 15
x -= 3 # x = x - 3
print(f"After x -= 3: {x}") # 12
x *= 2 # x = x * 2
print(f"After x *= 2: {x}") # 24
x /= 4 # x = x / 4
print(f"After x /= 4: {x}") # 6.0
# Membership Operators
print("\nMembership Operators:")
fruits = ["apple", "banana", "cherry"]
print(f"List: {fruits}")
print(f"'banana' in fruits: {'banana' in fruits}") # True
print(f"'orange' in fruits: {'orange' in fruits}") # False
print(f"'orange' not in fruits: {'orange' not in fruits}") # True
# String membership
text = "Hello World"
print(f"\nText: '{text}'")
print(f"'Hello' in text: {'Hello' in text}") # True
print(f"'Python' in text: {'Python' in text}") # False
Operator Precedence in Python
Operator precedence determines the order in which operations are performed when multiple operators are present in an expression.
# Operator Precedence Examples
# PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction
result1 = 5 + 3 * 2 # Multiplication before addition
print(f"5 + 3 * 2 = {result1}") # 11, not 16
result2 = (5 + 3) * 2 # Parentheses change precedence
print(f"(5 + 3) * 2 = {result2}") # 16
result3 = 2 ** 3 * 2 # Exponentiation before multiplication
print(f"2 ** 3 * 2 = {result3}") # 16 (8 * 2)
result4 = 2 * 3 ** 2 # Exponentiation before multiplication
print(f"2 * 3 ** 2 = {result4}") # 18 (2 * 9)
# Complex expression
result5 = 10 + 20 * 3 / 2 - 5 % 3
print(f"10 + 20 * 3 / 2 - 5 % 3 = {result5}")
# Steps: 20*3=60, 60/2=30, 5%3=2, 10+30=40, 40-2=38
Operator Precedence Table (Highest to Lowest)
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 | () |
Parentheses (grouping) | Left to Right |
| 2 | ** |
Exponentiation | Right to Left |
| 3 | +x, -x, ~x |
Unary plus, minus, bitwise NOT | Right to Left |
| 4 | *, /, //, % |
Multiplication, division, floor division, modulus | Left to Right |
| 5 | +, - |
Addition, subtraction | Left to Right |
| 6 | <<, >> |
Bitwise shifts | Left to Right |
| 7 | & |
Bitwise AND | Left to Right |
| 8 | ^ |
Bitwise XOR | Left to Right |
| 9 | | |
Bitwise OR | Left to Right |
| 10 | ==, !=, >, <, >=, <= |
Comparison operators | Left to Right |
| 11 | is, is not, in, not in |
Identity, membership operators | Left to Right |
| 12 | not |
Logical NOT | Right to Left |
| 13 | and |
Logical AND | Left to Right |
| 14 | or |
Logical OR | Left to Right |
(a + b) * c is clearer than a + b * c.
Practice Exercises
Try these exercises to test your understanding of Python operators.
# Python Operators Practice Exercises
print("=== Exercise 1: Arithmetic Operations ===")
# Calculate: (15 + 3 * 2) / (4 - 1)
result = (15 + 3 * 2) / (4 - 1)
print(f"(15 + 3 * 2) / (4 - 1) = {result}")
print("\n=== Exercise 2: Comparison Operators ===")
a = 10
b = 20
c = 10
print(f"a = {a}, b = {b}, c = {c}")
print(f"a == b: {a == b}")
print(f"a == c: {a == c}")
print(f"a != b: {a != b}")
print(f"a < b: {a < b}")
print("\n=== Exercise 3: Logical Operators ===")
age = 25
has_license = True
has_car = False
can_drive = age >= 18 and has_license
can_rent_car = age >= 21 and has_license
needs_ride = not has_car
print(f"Age: {age}, License: {has_license}, Car: {has_car}")
print(f"Can drive: {can_drive}")
print(f"Can rent car: {can_rent_car}")
print(f"Needs ride: {needs_ride}")
print("\n=== Exercise 4: Assignment Operators ===")
x = 10
print(f"Initial x: {x}")
x += 5 # What is x now?
print(f"After x += 5: {x}")
x *= 2 # What is x now?
print(f"After x *= 2: {x}")
x %= 7 # What is x now?
print(f"After x %= 7: {x}")
print("\n=== Exercise 5: Membership Operators ===")
fruits = ["apple", "banana", "orange", "grape"]
print(f"Fruits list: {fruits}")
print(f"'banana' in fruits: {'banana' in fruits}")
print(f"'mango' in fruits: {'mango' in fruits}")
print(f"'mango' not in fruits: {'mango' not in fruits}")
# String membership
sentence = "The quick brown fox jumps over the lazy dog"
print(f"\nSentence: '{sentence}'")
print(f"'fox' in sentence: {'fox' in sentence}")
print(f"'cat' in sentence: {'cat' in sentence}")
Key Takeaways
- Python has 7 types of operators: Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity
- Arithmetic operators (
+ - * / // % **) perform mathematical operations - Comparison operators (
== != > < >= <=) compare values and return Boolean - Logical operators (
and or not) combine Boolean expressions - Assignment operators (
= += -= *= /=) assign values to variables - Bitwise operators (
& | ^ ~ << >>) work on binary representations - Membership operators (
in not in) check if value exists in sequence - Identity operators (
is is not) check if two objects are same - Operator precedence determines evaluation order - use parentheses for clarity