Python Operators with Examples: Complete Guide for Beginners (2026)
In Python programming, operators are special symbols or keywords that perform operations on variables and values. They are the fundamental tools that allow you to manipulate data, make decisions, and control the flow of your program. Without operators, Python would just be a static list of variables.
In this comprehensive guide, we'll explore everything about Python operators including Arithmetic, Comparison (Relational), Logical, Bitwise, Assignment, Identity, Membership operators, and the all-important Operator Precedence table. We'll also cover common mistakes and real-world examples.
๐ Table of Contents
๐น What are Operators in Python?
An operator is a symbol that tells the Python interpreter to perform a specific mathematical, relational, or logical operation. The values that the operator acts on are called operands.
For example, in the expression 5 + 3, 5 and 3 are operands, and + is the operator.
๐น Arithmetic Operators
These are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 7 * 3 | 21 |
/ | Division (Float) | 10 / 3 | 3.333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 4 | 16 |
# Python Arithmetic Operators Examples
a = 17
b = 5
print(a + b) # Output: 22
print(a - b) # Output: 12
print(a * b) # Output: 85
print(a / b) # Output: 3.4
print(a // b) # Output: 3 (Floor division drops decimal)
print(a % b) # Output: 2 (Remainder of 17/5)
print(a ** 2) # Output: 289 (17 squared)
๐น Comparison (Relational) Operators
Comparison operators are used to compare two values. They always return a Boolean value: True or False. These are essential for if statements and loops.
| Operator | Name | Example |
|---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 10 > 4 → True |
< | Less than | 10 < 4 → False |
>= | Greater than or equal | 10 >= 10 → True |
<= | Less than or equal | 5 <= 2 → False |
# Python Comparison Operators
x = 15
y = 20
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= 15) # Output: True
print(y <= 15) # Output: False
๐น Logical Operators
Logical operators are used to combine conditional statements. Python uses English words (and, or, not) instead of symbols (&&, ||).
| Operator | Description | Example |
|---|---|---|
and | True if both are True | True and False → False |
or | True if at least one is True | True or False → True |
not | Reverses the result | not True → False |
# Python Logical Operators
age = 25
has_license = True
# and: Both conditions must be True
if age >= 18 and has_license:
print("You can drive!") # This will print
# or: At least one condition must be True
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("You can sleep in!") # This will print
# not: Inverts the boolean value
game_over = False
if not game_over:
print("Keep playing!") # This will print
๐น Bitwise Operators
Bitwise operators act on operands as if they were strings of binary digits (bits). They operate bit by bit, hence the name. These are crucial for low-level programming, cryptography, and network masking.
| Operator | Name | Example (a=10, b=4) |
|---|---|---|
& | AND | 10 & 4 → 0 (1010 & 0100 = 0000) |
| | OR | 10 | 4 → 14 (1010 | 0100 = 1110) |
^ | XOR | 10 ^ 4 → 14 (1010 ^ 0100 = 1110) |
~ | NOT (Complement) | ~10 → -11 |
<< | Left Shift | 10 << 2 → 40 |
>> | Right Shift | 10 >> 2 → 2 |
# Python Bitwise Operators
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(f"a & b = {a & b}") # Output: 0 (0000)
print(f"a | b = {a | b}") # Output: 14 (1110)
print(f"a ^ b = {a ^ b}") # Output: 14 (1110)
print(f"~a = {~a}") # Output: -11
print(f"a << 2 = {a << 2}") # Output: 40 (101000)
print(f"a >> 2 = {a >> 2}") # Output: 2 (0010)
๐น Assignment Operators
Assignment operators are used to assign values to variables. The most basic one is =. Python also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment.
# Basic Assignment
x = 10
print(x) # Output: 10
# Compound Assignment Operators
x += 5 # Equivalent to: x = x + 5
print(x) # Output: 15
x -= 3 # Equivalent to: x = x - 3
print(x) # Output: 12
x *= 2 # Equivalent to: x = x * 2
print(x) # Output: 24
x /= 4 # Equivalent to: x = x / 4
print(x) # Output: 6.0
x //= 2 # Equivalent to: x = x // 2
print(x) # Output: 3.0
x %= 2 # Equivalent to: x = x % 2
print(x) # Output: 1.0
x **= 3 # Equivalent to: x = x ** 3
print(x) # Output: 1.0
๐น Identity Operators
Identity operators (is and is not) check if two variables refer to the exact same object in memory, not just if they have the same value.
⚠️ Important: == checks for value equality. is checks for identity equality (same memory address).
# Identity Operators
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
# is: Returns True if both variables point to same object
print(list1 is list3) # Output: True (Same memory location)
print(list1 is list2) # Output: False (Different memory locations, same values)
# is not: Returns True if variables point to different objects
print(list1 is not list2) # Output: True
# ==: Checks value equality
print(list1 == list2) # Output: True (Values are identical)
๐น Membership Operators
Membership operators (in and not in) test whether a value or variable is found in a sequence (like a string, list, tuple, set, or dictionary).
# Membership Operators
fruits = ["apple", "banana", "cherry"]
text = "Hello Python Learners"
# in: Returns True if value exists in sequence
print("banana" in fruits) # Output: True
print("grape" in fruits) # Output: False
print("Python" in text) # Output: True
# not in: Returns True if value does NOT exist
print("grape" not in fruits) # Output: True
print("World" not in text) # Output: True
๐น Operator Precedence (PEMDAS in Python)
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.
Python Operator Precedence Table (Highest to Lowest)
| Precedence | Operator | Description |
|---|---|---|
| 1 (Highest) | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x, ~x | Unary plus, minus, bitwise NOT |
| 4 | *, /, //, % | Multiplication, Division, Floor, Mod |
| 5 | +, - | Addition and Subtraction |
| 6 | <<, >> | Bitwise Shift Operators |
| 7 | & | Bitwise AND |
| 8 | ^ | Bitwise XOR |
| 9 | | | Bitwise OR |
| 10 | ==, !=, >, <, >=, <= | Comparisons |
| 11 | is, is not, in, not in | Identity and Membership |
| 12 | not | Logical NOT |
| 13 | and | Logical AND |
| 14 (Lowest) | or | Logical OR |
# Demonstrating Operator Precedence
# Without parentheses: * has higher precedence than +
result1 = 10 + 5 * 2
print(result1) # Output: 20 (Not 30! Because 5*2=10, then 10+10=20)
# With parentheses: () overrides precedence
result2 = (10 + 5) * 2
print(result2) # Output: 30
# Complex example
result3 = 2 ** 3 + 5 * 2 - 8 / 4
# Step 1: ** (8) + 5*2 - 8/4
# Step 2: 8 + 10 - 2.0
# Step 3: 16.0
print(result3) # Output: 16.0
๐น Common Mistakes and How to Avoid Them
Even experienced developers stumble over these operator pitfalls. Here's how to sidestep them:
❌ Mistake 1: Confusing Assignment (=) with Equality (==)
This is the #1 bug for beginners. Using = inside an if statement assigns a value rather than checking equality.
# Wrong - Causes a syntax error or logical bug
x = 10
if x = 10: # SyntaxError: invalid syntax
print("Yes")
# Correct
if x == 10: # Comparison
print("Yes") # Output: Yes
❌ Mistake 2: Using is for Value Comparison
is checks identity, not equality. Python caches small integers (-5 to 256), so is might accidentally work in tests but fail in production.
# Deceptive behavior
a = 256
b = 256
print(a is b) # Output: True (Works because of integer caching!)
a = 257
b = 257
print(a is b) # Output: False (Surprise! Use == instead)
# Correct way to compare values
print(a == b) # Output: True
❌ Mistake 3: Misunderstanding Floor Division with Negative Numbers
Floor division (//) rounds towards negative infinity, not towards zero.
# Positive numbers behave as expected
print(10 // 3) # Output: 3
# Negative numbers surprise many developers
print(-10 // 3) # Output: -4 (Not -3! It floors down to -4)
print(10 // -3) # Output: -4
๐น Real-World Example: Building a Discount Calculator
# Combining Arithmetic, Comparison, and Logical Operators
def calculate_final_price(price, is_member, has_coupon):
discount = 0.0
# Using comparison and logical operators
if is_member or has_coupon:
discount += 0.10 # 10% base discount
if price > 100 and is_member:
discount += 0.05 # Extra 5% for members buying expensive items
final_price = price * (1 - discount)
return round(final_price, 2)
# Test the function
print(calculate_final_price(150, True, False)) # Output: 127.5
print(calculate_final_price(50, False, True)) # Output: 45.0
๐น Quick Reference Cheat Sheet
| Category | Operators |
|---|---|
| Arithmetic | + - * / // % ** |
| Comparison | == != > < >= <= |
| Logical | and or not |
| Bitwise | & | ^ ~ << >> |
| Assignment | = += -= *= /= %= //= **= &= |= ^= >>= <<= |
| Identity | is is not |
| Membership | in not in |
๐น Frequently Asked Questions (FAQ)
Q1: What is the difference between / and //?
✅ / performs float division (returns decimal). // performs floor division (returns integer rounded down).
Q2: Why does Python use 'and' instead of '&&'?
✅ Python prioritizes readability and uses English keywords for logical operations to make code cleaner and more intuitive.
Q3: What is the difference between = and ==?
✅ = is an assignment operator (assigns a value). == is a comparison operator (checks equality).
Q4: What does the ** operator do?
✅ It's the exponentiation operator. 2 ** 3 means 2 raised to the power of 3 (equals 8).
Q5: When should I use 'is' instead of '=='?
✅ Use is only when comparing with singletons like None, True, or False. Use == for comparing values.
Q6: What is the order of operations in Python?
✅ Follow the PEMDAS rule: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. Logical operators come last.
๐งช Test Your Knowledge: Python Operator Challenge
What will be the output of this code? (Comment your answer below!)
x = 5
y = 2
print(x ** y + (10 // 3) * (10 > 5))
๐ Click to Reveal Answer & Explanation
Answer: 28.0
Step 1: x ** y → 5 ** 2 = 25
Step 2: 10 // 3 → 3
Step 3: 10 > 5 → True (which equals 1 in arithmetic)
Step 4: 3 * 1 = 3
Step 5: 25 + 3 = 28.0 (Python upgrades to float due to division behavior in some contexts, or prints 28).
*Note: Boolean True acts as integer 1.