Python Operators – A Complete Guide for Beginners (All 7 Types Explained)
Python Operators – A Complete Guide for Beginners (All 7 Types Explained)
When you write a Python program, you do not just store values — you perform actions on them. Adding numbers, comparing values, combining conditions, checking membership — all of these are done using operators. In this complete guide, you will learn all 7 types of Python operators with clear syntax, tables, code examples with outputs, and real-life use cases — everything you need to use operators confidently.
Table of Contents
- What are Operators in Python?
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
- Operator Precedence
- FAQ
- What are Operators in Python?
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
- Operator Precedence
- FAQ
1. What are Operators in Python?
An operator is a special symbol that tells Python what action to perform on one or more values. The values that an operator works on are called operands.
a = 10 b = 5 print(a + b) # + is the operator, a and b are operands
15
Python has 7 main categories of operators. Here is a quick overview before we dive into each one:
2. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations — addition, subtraction, multiplication, division, and more.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (float) | 10 / 3 | 3.333... |
| // | Floor Division (integer) | 10 // 3 | 3 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation (power) | 2 ** 3 | 8 |
a = 10 b = 3 print(a + b) # 13 print(a - b) # 7 print(a * b) # 30 print(a / b) # 3.3333333333333335 print(a // b) # 3 (floor – rounds down) print(a % b) # 1 (remainder) print(a ** b) # 1000 (10 to the power 3)
13 7 30 3.3333333333333335 3 1 1000
// (floor division) always rounds down to the nearest integer. 10 // 3 gives 3, not 3.33. Use % to find the remainder — great for checking even/odd or divisibility.Real-life use of arithmetic operators
# Calculate total bill with discount
price = 1200
discount = 200
gst = 18
net_price = price - discount
tax = (net_price * gst) // 100
total = net_price + tax
print(f"Net Price: ₹{net_price}")
print(f"GST (18%): ₹{tax}")
print(f"Total Bill: ₹{total}")
Net Price: ₹1000
GST (18%): ₹180
Total Bill: ₹1180
# Calculate total bill with discount price = 1200 discount = 200 gst = 18 net_price = price - discount tax = (net_price * gst) // 100 total = net_price + tax print(f"Net Price: ₹{net_price}") print(f"GST (18%): ₹{tax}") print(f"Total Bill: ₹{total}")
Net Price: ₹1000 GST (18%): ₹180 Total Bill: ₹1180
3. Assignment Operators
Assignment operators are used to assign or update the value of a variable. The basic assignment operator is =. The compound operators like += and -= combine an operation with assignment — they are shorthand for writing a = a + 3 as a += 3.
| Operator | Meaning | Equivalent to |
|---|---|---|
| = | Assign value | a = 5 |
| += | Add and assign | a = a + 3 |
| -= | Subtract and assign | a = a - 3 |
| *= | Multiply and assign | a = a * 3 |
| /= | Divide and assign | a = a / 3 |
| //= | Floor divide and assign | a = a // 3 |
| %= | Modulus and assign | a = a % 3 |
| **= | Exponent and assign | a = a ** 2 |
x = 10 print(f"Start: {x}") x += 5 print(f"After += 5: {x}") x -= 3 print(f"After -= 3: {x}") x *= 2 print(f"After *= 2: {x}") x //= 4 print(f"After //= 4: {x}") x **= 2 print(f"After **= 2: {x}")
Start: 10 After += 5: 15 After -= 3: 12 After *= 2: 24 After //= 4: 6 After **= 2: 36
4. Comparison (Relational) Operators
Comparison operators compare two values and always return either True or False. They are the backbone of all condition-based logic — used heavily in if statements, loops, and filters.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 10 > 5 | True |
| < | Less than | 3 < 7 | True |
| >= | Greater than or equal | 5 >= 5 | True |
| <= | Less than or equal | 4 <= 6 | True |
x = 5 y = 10 print(x == y) # False print(x != y) # True print(x > y) # False print(x < y) # True print(x >= y) # False print(x <= y) # True
False True False True False True
= (assignment) with == (comparison). Writing if x = 5 causes a SyntaxError. Always use == to compare values inside conditions.Comparison in real programs
marks = 72
pass_mark = 40
if marks >= pass_mark:
print("Passed")
else:
print("Failed")
Passed
marks = 72 pass_mark = 40 if marks >= pass_mark: print("Passed") else: print("Failed")
Passed
5. Logical Operators
Logical operators are used to combine two or more conditions together. They are most commonly used inside if statements when you need to check multiple things at once.
| Operator | Meaning | Returns True when |
|---|---|---|
| and | Logical AND | Both conditions are True |
| or | Logical OR | At least one condition is True |
| not | Logical NOT | The condition is False (reverses it) |
age = 20 has_id = True print(age >= 18 and has_id) # True (both are True) print(age >= 18 or has_id) # True (at least one is True) print(not has_id) # False (reverses True)
True True False
# Real use: Check eligibility salary = 35000 experience = 3 if salary >= 30000 and experience >= 2: print("Eligible for loan") else: print("Not eligible")
Eligible for loan
and when ALL conditions must be True. Use or when ANY one condition being True is enough. Use not to flip a True to False or False to True.6. Bitwise Operators
Bitwise operators work directly on the binary (bit-level) representation of numbers. They are used in low-level programming, encryption, image processing, and performance optimization. Each number is first converted to binary, then the operation is applied bit by bit.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| & | Bitwise AND | 5 & 3 | 1 |
| | | Bitwise OR | 5 | 3 | 7 |
| ^ | Bitwise XOR | 5 ^ 3 | 6 |
| ~ | Bitwise NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 5 >> 1 | 2 |
a = 5 # Binary: 0101 b = 3 # Binary: 0011 print(a & b) # AND: 1 (0001) print(a | b) # OR: 7 (0111) print(a ^ b) # XOR: 6 (0110) print(~a) # NOT: -6 (two's complement) print(a << 1) # Left shift: 10 (1010) print(a >> 1) # Right shift: 2 (0010)
1 7 6 -6 10 2
How bitwise AND and OR work — step by step
AND operation: 5 & 3 → 1 (both bits must be 1)a = 50101b = 30011result0001= 1OR operation: 5 | 3 → 7 (at least one bit must be 1)a = 50101b = 30011result0111= 7XOR operation: 5 ^ 3 → 6 (bits must be different)a = 50101b = 30011result0110= 6Left shift << n multiplies the number by 2ⁿ. Right shift >> n divides by 2ⁿ (integer division). So 5 << 1 = 10 and 5 >> 1 = 2.
<< n multiplies the number by 2ⁿ. Right shift >> n divides by 2ⁿ (integer division). So 5 << 1 = 10 and 5 >> 1 = 2.7. Membership Operators
Membership operators test whether a value exists inside a sequence such as a list, tuple, string, or set. They return True or False and are very commonly used in real-world Python programs.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| in | Value exists in sequence | 'a' in 'cat' | True |
| not in | Value does not exist | 'z' not in 'cat' | True |
fruits = ["apple", "banana", "mango"] print("apple" in fruits) # True print("orange" in fruits) # False print("grape" not in fruits) # True # Also works with strings print("Py" in "Python") # True print("java" in "Python") # False
True False True True False
# Real use: Check allowed users allowed_users = ["admin", "rohan", "priya"] username = "rohan" if username in allowed_users: print("Access granted") else: print("Access denied")
Access granted
8. Identity Operators
Identity operators check whether two variables point to the exact same object in memory — not just whether they have equal values. This is a subtle but important difference.
| Operator | Meaning | Returns True when |
|---|---|---|
| is | Same object in memory | Both variables point to same object |
| is not | Different object in memory | Variables point to different objects |
x = [1, 2, 3] y = [1, 2, 3] z = x # z points to the SAME object as x print(x is z) # True (same object) print(x is y) # False (different objects, same content) print(x == y) # True (same content/value) print(x is not y) # True (different objects)
True False True True
is checks memory location (are they the same object?). == checks value equality (do they have the same content?). Two variables can have equal values but still be different objects — use == for value comparison in most programs.9. Operator Precedence
When an expression has multiple operators, Python follows a specific order called operator precedence — similar to the BODMAS rule in mathematics. Operators with higher precedence are evaluated first.
result1 = 2 + 3 * 4 # * has higher precedence than + result2 = (2 + 3) * 4 # () evaluated first print(result1) # 14 print(result2) # 20
14 20
Here is the precedence order from highest to lowest:
| Priority | Operator(s) | Description |
|---|---|---|
| 1 (highest) | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x, ~x | Unary operators |
| 4 | *, /, //, % | Multiplication, division, modulus |
| 5 | +, - | Addition, subtraction |
| 6 | <<, >> | Bitwise shifts |
| 7 | & | Bitwise AND |
| 8 | ^ | Bitwise XOR |
| 9 | | | Bitwise OR |
| 10 | ==, !=, <, >, <=, >=, is, in | Comparisons, identity, membership |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 (lowest) | or | Logical OR |
() to make the order explicit. It also makes your code easier to read for others.- Arithmetic –
+ - * / // % **for math calculations - Assignment –
= += -= *=etc. to assign or update variables - Comparison –
== != > < >= <=return True or False - Logical –
and or notcombine or reverse conditions - Bitwise –
& | ^ ~ << >>operate on binary bits - Membership –
in not incheck if value exists in a sequence - Identity –
is is notcheck if two variables are the same object - Use parentheses to control the order when multiple operators appear together
Frequently Asked Questions (FAQ)
Q1. What is the difference between == and = in Python?
= is the assignment operator — it stores a value in a variable. == is the comparison operator — it checks if two values are equal and returns True or False. Using = inside an if condition causes a SyntaxError.
Q2. What is the difference between / and // in Python?
/ is regular division and always returns a float, even if the result is a whole number (e.g. 10/2 = 5.0). // is floor division and returns an integer by rounding down (e.g. 10//3 = 3, not 3.33).
Q3. What is the difference between is and == in Python?
== checks if two values are equal. is checks if two variables point to the exact same object in memory. Two variables can have equal values (== True) but still be different objects (is False).
Q4. When should I use logical operators vs bitwise operators?
Use logical operators (and, or, not) for combining boolean conditions in if statements and loops. Use bitwise operators (&, |, ^) when working directly with binary data, flags, or low-level operations.
Q5. What does the % operator do in Python?
The % operator returns the remainder of a division. For example, 10 % 3 = 1 because 10 divided by 3 gives 3 with a remainder of 1. It is commonly used to check if a number is even (n % 2 == 0) or divisible by something.
Q6. Can membership operators be used with strings?
Yes. The in and not in operators work with strings, lists, tuples, sets, and dictionaries. For strings, they check whether a substring exists — for example, "Py" in "Python" returns True.
Q1. What is the difference between == and = in Python?
= is the assignment operator — it stores a value in a variable. == is the comparison operator — it checks if two values are equal and returns True or False. Using = inside an if condition causes a SyntaxError.
Q2. What is the difference between / and // in Python?
/ is regular division and always returns a float, even if the result is a whole number (e.g. 10/2 = 5.0). // is floor division and returns an integer by rounding down (e.g. 10//3 = 3, not 3.33).
Q3. What is the difference between is and == in Python?
== checks if two values are equal. is checks if two variables point to the exact same object in memory. Two variables can have equal values (== True) but still be different objects (is False).
Q4. When should I use logical operators vs bitwise operators?
Use logical operators (and, or, not) for combining boolean conditions in if statements and loops. Use bitwise operators (&, |, ^) when working directly with binary data, flags, or low-level operations.
Q5. What does the % operator do in Python?
The % operator returns the remainder of a division. For example, 10 % 3 = 1 because 10 divided by 3 gives 3 with a remainder of 1. It is commonly used to check if a number is even (n % 2 == 0) or divisible by something.
Q6. Can membership operators be used with strings?
Yes. The in and not in operators work with strings, lists, tuples, sets, and dictionaries. For strings, they check whether a substring exists — for example, "Py" in "Python" returns True.
Found this post helpful? Share it with someone learning Python. Drop your questions in the comments — happy to help!
"Every Python expert once struggled with the basics. Learn slowly, practise daily, and trust the process."
Comments
Post a Comment