If variables are the nouns of Python, then operators are the verbs. They help Python perform actions — add two numbers, compare values, or combine conditions.

Understanding operators is crucial to writing logic that works. Whether you’re building a calculator, validating passwords, or making games — you’ll be using Python’s operators everywhere.
In this guide, we’ll walk through:
- What are operators?
- Different types of operators in Python
- Real-life examples for each
- A few gotchas to avoid
- A mini practice task at the end
What are Operators?
An operator is a special symbol or keyword that performs a specific operation on values or variables.
For example:
a = 10
b = 5
print(a + b) # ➝ 15
Here, +
is an operator that adds a
and b
.
1. Arithmetic Operators
These are the basic math operators:
Operator | Meaning | Example | Output |
---|---|---|---|
+ | Addition | 3 + 2 | 5 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 4 * 3 | 12 |
/ | Division | 10 / 2 | 5.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Example:
amount = 1050
tax = 0.18
total = amount + (amount * tax)
print("Total payable:", total)
Also Read: Python Variables and Data Types
2. Comparison Operators
These help you compare values and return a Boolean (True
or False
).
Operator | Meaning | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 5 < 3 | False |
>= | Greater or equal | 10 >= 10 | True |
<= | Less or equal | 3 <= 2 | False |
Example:
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote!")
else:
print("Sorry, you must be 18 or older.")
3. Logical Operators
Used to combine multiple conditions.
Operator | Meaning | Example | Output |
---|---|---|---|
and | True if both are true | 5 > 3 and 4 > 2 | True |
or | True if at least one is true | 5 > 3 or 4 < 2 | True |
not | Inverts result | not (5 > 3) | False |
Example:
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login successful!")
else:
print("Login failed.")
4. Assignment Operators
These are shortcuts to assign values.
Operator | Meaning | Example | Same As |
---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 2 | x = x - 2 |
*= | Multiply and assign | x *= 4 | x = x * 4 |
/= | Divide and assign | x /= 2 | x = x / 2 |
//= | Floor divide and assign | x //= 3 | x = x // 3 |
%= | Modulus and assign | x %= 2 | x = x % 2 |
**= | Exponent and assign | x **= 2 | x = x ** 2 |
5. Membership Operators
Used to check whether a value exists in a sequence (like a list, string, etc.)
Operator | Meaning | Example | Output |
---|---|---|---|
in | Present in sequence | "a" in "smarttejas" | True |
not in | Not present | 5 not in [1,2,3] | True |
Example:
email = input("Enter your email: ")
if "@" in email:
print("Valid email")
else:
print("Invalid email")
6. Identity Operators
Used to compare object identities (more advanced but good to know).
Operator | Meaning | Example | Output |
---|---|---|---|
is | Same object | x is y | True/False |
is not | Not same object | x is not y | True/False |
a = [1, 2]
b = a
print(a is b) # True (both point to the same object)
Gotchas to Watch Out For
=
vs==
=
assigns a value==
checks equality- 👉 Always use
==
in comparisons.
- Integer division:
10 / 3 ➝ 3.33
10 // 3 ➝ 3
(no decimals)
- Operator precedence matters!
2 + 3 * 4 ➝ 14
, not20
- Use parentheses for clarity:
(2 + 3) * 4
Try It Yourself: Mini Practice
Create a Python program that:
- Takes the number of travel days as input
- Asks for the daily cost of:
- Food
- Travel
- Hotel stay
- Calculates the total trip cost using arithmetic operations
- Displays the final result using
print()
It’s a great way to review arithmetic, comparison, and logical operators all in one mini-project!
Conclusion
Operators are the building blocks of logic in Python. They’re small symbols, but they hold massive power.
Whether you’re doing math, checking passwords, validating inputs, or writing conditions — you’re using operators under the hood.