In Python, operators are special symbols or keywords used to perform operations on variables and values. Think of them as tools that let your code calculate, compare, or make decisions.
This guide will walk you through the three core types of basic operators in Python:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
Let’s explore each one with real-world scenarios and easy-to-understand examples.
🧮 1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations, just like you do on a calculator.
Operator | Name | Description | Example | Result |
---|---|---|---|---|
+ | Addition | Adds two values | 5 + 3 | 8 |
- | Subtraction | Subtracts right value from left | 10 - 2 | 8 |
* | Multiplication | Multiplies two values | 4 * 6 | 24 |
/ | Division | Divides left by right (returns float) | 9 / 2 | 4.5 |
// | Floor Division | Divides and returns the whole number only | 9 // 2 | 4 |
% | Modulus | Returns the remainder | 10 % 3 | 1 |
** | Exponent | Raises to power | 2 ** 3 | 8 |
📌 Example Code:
a = 12
b = 5
print("Addition:", a + b) # 17
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 60
print("Division:", a / b) # 2.4
print("Floor Division:", a // b) # 2
print("Modulus:", a % b) # 2
print("Exponent:", a ** b) # 248832
💡 Real-life Analogy:
If you have 10 candies and give 3 to each of your 3 friends:
- You’ve given away:
3 * 3 = 9
candies - You’ll have:
10 - 9 = 1
candy left → This is where the modulus (%) operator helps!
🔍 2. Comparison Operators
These operators are used to compare two values. The result is always either True
or False
. They are mostly used in if
statements, loops, or condition checks.
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 8 > 6 | True |
< | Less than | 4 < 2 | False |
>= | Greater than or equal to | 7 >= 7 | True |
<= | Less than or equal to | 5 <= 8 | True |
📌 Example Code:
age = 18
print(age == 18) # True
print(age != 21) # True
print(age > 16) # True
print(age < 16) # False
print(age >= 18) # True
print(age <= 17) # False
💡 Real-life Analogy:
If you’re checking if someone is old enough to vote:
age = 17
if age >= 18:
print("You can vote")
else:
print("You cannot vote yet")
🔗 3. Logical Operators
Logical operators are used to combine multiple conditions. They help in decision-making by evaluating the truthiness of multiple expressions.
Operator | Meaning | Usage | Result |
---|---|---|---|
and | True if both conditions are true | True and True | True |
or | True if at least one condition is true | True or False | True |
not | Reverses the result | not True | False |
📌 Example Code:
age = 20
has_id = True
# Using 'and'
if age >= 18 and has_id:
print("You can enter the club") # True
# Using 'or'
if age >= 18 or has_id:
print("You can still enter")
# Using 'not'
is_banned = False
if not is_banned:
print("Access granted")
💡 Real-life Analogy:
Imagine a login system:
- You can only log in if username and password are correct → Use
and
- A coupon is applied if the user is a student or a first-time buyer → Use
or
- A user is allowed if they are not banned → Use
not
✅ Final Summary
Type | Used For | Examples |
---|---|---|
Arithmetic | Math operations | + , - , * , / , % , ** , // |
Comparison | Check values | == , != , < , > , <= , >= |
Logical | Combine conditions | and , or , not |
🧠 Practice Tips:
- Try building a simple calculator using arithmetic operators.
- Create a login or age-checking system using comparison and logical operators.
- Use these in if-else conditions to get a feel for real-world logic in programming.
Let me know if you’d like me to turn this into a PDF guide or tutorial post!