python operators
Python Operators
What are Operators?
Operators are special symbols that perform operations on values and variables.
Example:
a = 10
b = 5
print(a + b)
Output:
15
Here:
+
is an operator.
Types of Operators in Python
Python provides the following operators:
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Identity Operators
- Membership Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
Assume:
a = 10
b = 3
Addition (+)
print(a + b)
Output:
13
Subtraction (-)
print(a - b)
Output:
7
Multiplication (*)
print(a * b)
Output:
30
Division (/)
Always returns float.
print(a / b)
Output:
3.3333333333333335
Floor Division (//)
Returns integer quotient.
print(a // b)
Output:
3
Modulus (%)
Returns remainder.
print(a % b)
Output:
1
Exponentiation (**)
Power operator.
print(a ** b)
Output:
1000
Real Life Example
Calculate Total Bill
item_price = 250
quantity = 4
total = item_price * quantity
print(total)
Output:
1000
Calculate GST
amount = 1000
gst = amount * 18 / 100
print(gst)
Output:
180.0
Common Arithmetic Mistakes
Mistake 1
❌ Wrong
print(10 / 2)
Expected:
5
Actual:
5.0
Reason:
Division always returns float.
Mistake 2
❌ Wrong
print(10 ^ 2)
Expected:
100
Actual:
8
Reason:
^ is Bitwise XOR.
Use:
10 ** 2
2. Assignment Operators
Used to assign values.
Simple Assignment (=)
x = 10
Add and Assign (+=)
x = 10
x += 5
print(x)
Output:
15
Subtract and Assign (-=)
x = 10
x -= 3
print(x)
Output:
7
Multiply and Assign (*=)
x = 10
x *= 2
print(x)
Output:
20
Divide and Assign (/=)
x = 10
x /= 2
print(x)
Output:
5.0
Floor Divide and Assign (//=)
x = 10
x //= 3
print(x)
Output:
3
Modulus and Assign (%=)
x = 10
x %= 3
print(x)
Output:
1
Power and Assign (**=)
x = 2
x **= 3
print(x)
Output:
8
Real Life Example
Bank Balance
balance = 1000
balance += 500
print(balance)
Output:
1500
Common Assignment Mistakes
Mistake
❌ Wrong
x + 5
Expected:
x becomes larger
Actual:
Nothing changes
Correct:
x += 5
3. Comparison (Relational) Operators
Used to compare values.
Returns:
True
or
False
Assume:
a = 10
b = 20
Equal To (==)
print(a == b)
Output:
False
Not Equal To (!=)
print(a != b)
Output:
True
Greater Than (>)
print(b > a)
Output:
True
Less Than (<)
print(a < b)
Output:
True
Greater Than or Equal To (>=)
print(a >= 10)
Output:
True
Less Than or Equal To (<=)
print(a <= 10)
Output:
True
Real Life Example
Voting Eligibility
age = 18
print(age >= 18)
Output:
True
Common Comparison Mistakes
Mistake
❌ Wrong
if age = 18:
Output:
SyntaxError
Correct:
if age == 18:
4. Logical Operators
Used to combine conditions.
AND Operator
Returns True only if all conditions are True.
age = 25
citizen = True
print(age >= 18 and citizen)
Output:
True
OR Operator
Returns True if at least one condition is True.
print(True or False)
Output:
True
NOT Operator
Reverses the result.
print(not True)
Output:
False
Real Life Example
Login System
username = "admin"
password = "123"
print(
username == "admin"
and password == "123"
)
Output:
True
Common Logical Mistakes
Mistake
❌ Wrong
if age > 18 or age < 60:
This is almost always True.
Correct:
if age > 18 and age < 60:
5. Bitwise Operators
Bitwise operators work on binary numbers.
Understanding Binary
Decimal:
10
Binary:
1010
Decimal:
6
Binary:
0110
Assume:
a = 10
b = 6
Bitwise AND (&)
print(a & b)
Calculation:
1010
0110
----
0010
Output:
2
Bitwise OR (|)
print(a | b)
Calculation:
1010
0110
----
1110
Output:
14
Bitwise XOR (^)
print(a ^ b)
Output:
12
Bitwise NOT (~)
print(~10)
Output:
-11
Left Shift (<<)
print(10 << 1)
Output:
20
Explanation:
1010 → 10100
Right Shift (>>)
print(10 >> 1)
Output:
5
Explanation:
1010 → 0101
Real Life Examples of Bitwise Operators
Permission System
READ = 1
WRITE = 2
DELETE = 4
user_permission = READ | WRITE
print(user_permission)
Output:
3
Check permission:
print(user_permission & WRITE)
Output:
2
Feature Flags
DARK_MODE = 1
NOTIFICATION = 2
settings = DARK_MODE | NOTIFICATION
print(settings)
Output:
3
Common Bitwise Mistakes
Mistake
❌ Wrong
10 ^ 2
Expected:
100
Actual:
8
Use:
10 ** 2
for power.
6. Identity Operators
Checks whether two variables refer to the same object.
is
a = [1, 2]
b = a
print(a is b)
Output:
True
is not
a = [1, 2]
b = [1, 2]
print(a is not b)
Output:
True
Difference Between == and is
Equality (==)
Checks values.
a = [1, 2]
b = [1, 2]
print(a == b)
Output:
True
Identity (is)
Checks memory location.
print(a is b)
Output:
False
Common Identity Mistake
❌ Wrong
if age is 18:
Correct:
if age == 18:
Use:
is
mainly for:
None
Example:
if value is None:
print("No value")
7. Membership Operators
Used to check existence.
in
name = "Python"
print("P" in name)
Output:
True
not in
name = "Python"
print("Z" not in name)
Output:
True
Real Life Example
User Search
users = [
"John",
"Alice",
"Bob"
]
print("Alice" in users)
Output:
True
Common Membership Mistake
❌ Wrong
print("10" in [10,20,30])
Output:
False
Reason:
String and integer are different types.
Operator Precedence
Python follows order:
()
**
*, /, //, %
+, -
<, <=, >, >=
==, !=
not
and
or
Example
print(2 + 3 * 4)
Output:
14
Use parentheses:
print((2 + 3) * 4)
Output:
20
Homework 1
Calculate:
25 + 10 * 2
Solution
print(25 + 10 * 2)
Output:
45
Homework 2
Check if a person is eligible to vote.
Condition:
age >= 18
Age:
21
Solution
age = 21
print(age >= 18)
Output:
True
Homework 3
Find remainder when:
50
is divided by:
7
Solution
print(50 % 7)
Output:
1
Homework 4
Check whether:
"Python"
contains:
"th"
Solution
print("th" in "Python")
Output:
True
Homework 5
Create permissions:
READ = 1
WRITE = 2
Combine them using Bitwise OR.
Solution
READ = 1
WRITE = 2
permission = READ | WRITE
print(permission)
Output:
3
Homework 6
Find difference between:
==
and
is
using lists.
Solution
a = [1, 2]
b = [1, 2]
print(a == b)
print(a is b)
Output:
True
False
Summary
| Operator Type | Operators |
|---|---|
| Arithmetic | + - * / // % ** |
| Assignment | = += -= *= /= //= %= **= |
| Comparison | == != > < >= <= |
| Logical | and or not |
| Bitwise | & | ^ ~ << >> |
| Identity | is, is not |
| Membership | in, not in |
Before moving ahead, be comfortable with:
- Variables
- Data Types
- Type Casting
- String Methods
- Operators
These form the foundation for:
- Conditional Statements (
if,elif,else) - Loops (
for,while) - Functions
- Exception Handling
- Object-Oriented Programming