🧮 Python Operators Explained with Examples (Beginner Friendly Guide)
Python Operators
Now that you understand Python variables and data types, it’s time to learn how to work with them. Storing data is useful — but the real power of programming comes when you start performing operations on that data.
This is where Python operators come in.
Operators allow you to:
-
Perform calculations
-
Compare values
-
Combine conditions
-
Assign new values
-
Check logical conditions
In this lesson, you’ll learn all major Python operators with simple examples and real-life explanations.
What Are Operators in Python?
An operator is a symbol that performs an operation on variables and values.
For example:
a = 10
b = 5
print(a + b)
Here, + is an operator that adds two numbers.
Output:
15
Simple, right? Let’s explore all types step by step.
1️⃣ Arithmetic Operators (Math Operations)
These operators are used for mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 3 |
| - | Subtraction | 5 - 3 |
| * | Multiplication | 5 * 3 |
| / | Division | 10 / 2 |
| % | Modulus (remainder) | 10 % 3 |
| ** | Exponent (power) | 2 ** 3 |
| // | Floor division | 10 // 3 |
Example Program:
a = 10
b = 3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Remainder:", a % b)
print("Power:", a ** b)
print("Floor Division:", a // b)
Real-Life Example
Imagine you're building a shopping bill calculator:
-
+adds item prices -
-applies discounts -
%calculates tax percentage -
//calculates number of full boxes
Arithmetic operators are used everywhere!
2️⃣ Comparison Operators (Relational Operators)
These operators compare two values and return True or False.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example:
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
These are very important in decision-making (if-else statements).
3️⃣ Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Meaning |
|---|---|
| and | True if both conditions are true |
| or | True if at least one condition is true |
| not | Reverses the result |
Example:
age = 20
has_id = True
print(age >= 18 and has_id) # True
print(age < 18 or has_id) # True
print(not has_id) # False
Real-Life Example:
Checking if someone can vote:
-
Age must be 18+
-
Must have ID
Logical operators make such checks possible.
4️⃣ Assignment Operators
Assignment operators assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
| = | x = 5 | Assign value |
| += | x += 3 | x = x + 3 |
| -= | x -= 2 | x = x - 2 |
| *= | x *= 4 | x = x * 4 |
| /= | x /= 2 | x = x / 2 |
Example:
x = 10
x += 5
print(x) # 15
x *= 2
print(x) # 30
These make code shorter and cleaner.
5️⃣ Identity Operators
Identity operators check whether two variables refer to the same object in memory.
| Operator | Meaning |
|---|---|
| is | True if same object |
| is not | True if not same object |
Example:
a = 5
b = 5
print(a is b) # True
Used more in advanced Python concepts.
6️⃣ Membership Operators
These check whether a value exists inside a sequence (like list, string, tuple).
| Operator | Meaning |
|---|---|
| in | True if value exists |
| not in | True if value does not exist |
Example:
fruits = ["apple", "banana", "mango"]
print("apple" in fruits) # True
print("grapes" not in fruits) # True
Very useful when working with lists and strings.
Mini Project: Simple Login Checker
Let’s combine operators in a small program:
username = "admin"
password = "1234"
input_user = input("Enter username: ")
input_pass = input("Enter password: ")
if input_user == username and input_pass == password:
print("Login Successful!")
else:
print("Invalid Credentials!")
Here we used:
-
Comparison operators (
==) -
Logical operator (
and) -
Conditional statement (
if-else)
This is how real applications work!
Common Beginner Mistakes
❌ Using = instead of == in comparisons
❌ Forgetting operator priority
❌ Mixing data types (like string + number without conversion)
Example mistake:
age = "18"
print(age + 2) # Error
Correct way:
age = int("18")
print(age + 2)
Operator Precedence (Order of Operations)
Python follows a specific order:
-
Parentheses ()
-
Exponent **
-
Multiplication/Division
-
Addition/Subtraction
-
Comparison
-
Logical operators
Example:
result = 10 + 5 * 2
print(result) # 20 (not 30)
Because multiplication happens first.
Conclusion
Python operators are the engine of programming logic. They allow you to:
-
Perform calculations
-
Compare values
-
Combine conditions
-
Assign and modify data
-
Build real-world applications
Once you master operators, you are ready to move to the next powerful concept — Conditional Statements (if, elif, else).
Keep practicing small programs daily — that’s the secret to mastering Python. 🚀
Python Variables and Data Types
Comments