Welcome to Day 4 of our Learn Python in 30 Days challenge! Todayโs focus is Python operators for beginners โ the tools that allow Python to perform calculations, comparisons, and logical decisions.
๐ Missed earlier parts? Catch up here:
- Day 1: Learn Python in 30 Days โ Part 1: What is Python and How to Start (With Real-Life Analogies)
- Day 2: Master Python Variables and Data Types โ Day 2 of the 30-Day Learning challenge
- Day 3: Day 3: Input and Output in Python โ Learn How to Talk to Your Code(Part 3)
โ 1. Arithmetic Operators in Python
Arithmetic operators in Python help you perform basic mathematical operations, just like a calculator – but inside your code. Whether you want to add marks, calculate averages, or divide money equally among friends, these operators make it easy.
Here are the most common ones:
Operator | Function | Example |
---|---|---|
+ | Addition | 4 + 2 = 6 |
– | Subtraction | 5 – 3 = 2 |
* | Multiplication | 3 * 2 = 6 |
/ | Division | 10 / 2 = 5.0 |
// | Floor Division | 10 // 3 = 3 |
% | Modulus | 10 % 3 = 1 |
** | Exponent (Power) | 2 ** 3 = 8 |
math_score = 90
science_score = 85
total= math_score + science_score
average= total / 2
print("Total marks:", total)
print("Average marks:, average)
Output
Total marks: 175
Average marks: 87.5
These are the operators that form the foundation of any programming logic – whether it’s building a calculator, budgeting app, or even a game.
๐ 2. Assignment Operators โ Storing and Updating Values
Assignment operators help Python remember values by storing them in Variables. Think of them as the “equal to” button on a calculator – but more powerful. You can also update a variable using shorthand.
Operators | Meaning | Example | Same As |
= | Assign value | x = 10 | – |
+= | Add & assign | x += 5 | x = x + 5 |
-= | Subtract & assign | x -= 3 | x = x – 3 |
*= | Multiply & assign | x *= 2 | x = x * 2 |
/= | Divide & assign | X /= 4 | x = x / 4 |
coins = 10
coins += 5 #You found 5 more coins
coins -= 2. #You spent 2 coins
print("Remaining coins:", coins)
Output
Remaining coins: 13
Assignment operators are a clean and efficient way to update variables without repeating yourself.
โ 3. Comparison Operators โ Asking Python to Compare Things
Comparison operators help you ask questions in your code like:
- “Is this number bigger than that one?”
- “Are these two values equal?”
They’re used in if statements and conditions – where Python needs to decide what to do.
Operator | Meaning | Example |
---|---|---|
== | Equal to | 4 == 4 โ True |
!= | Not equal to | 4 != 5 โ True |
> | Greater than | 7 > 5 โ True |
< | Less than | 2 < 6 โ True |
>= | Greater or equal | 5 >= 5 โ True |
<= | Less or equal | 3 <= 2 โ False |
age = 16
if age >= 18:
print("You can vote!")
else:
print("You are too young to vote.")
Output
You are too young to vote.
These operators help Python make decisions and are used heavily in real-world program like login systems, exam results, checks, and games.
๐ You May Also Like:
๐ง 4. Logical Operators โ Making Smart Decisions
Logical operators in Python help your program make multiple decisions at once. Instead of checking one condition, you can check two or more together.
They answer questions like:
- “Are both conditions true?”
- “Is at least on condition true?”
- “Is this condition not true?”
Python has 3 logical operators: and, or, not
Operator | Description |
---|---|
and | Returns True if both conditions are True |
or | Returns True if at least one condition is True |
not | Reverses the result |
1 Both conditions must be true
age = 18
citizen = True
if age >= 18 and citizen:
print("You can vote.")
else:
print("You can not vote.")
Output: You can vote.
Used when both conditions must be true
2 At least one condition must be true
has_id = False
has_passport = True
if has_id or has_passport:
print("You can enter the exam hall.")
else:
print("Access denied.")
Output: You can enter the exam hall.
Used when any one of the conditions being true is enough.
3. Reverse the result
is_raining = False
if not is_raining:
print("Let's go outside!")
else:
print("Stay indoors.")
Output: Let’s go outside!
Used when you want to check if something is not true.
Real-Life Example (All 3 Together):
username = "admin"
password = "1234"
logged_in = True
if username == "admin" and password == "1234" and logged_in:
print("Welcome to your dashboard!")
else:
print("Access denied.")
Logical operators make your code smarter and are heavily used in:
- Login systems
- Games
- Form validations
- Conditional calculations
๐ Recap: What You Learned in Day 4
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
๐ฎ Whatโs Next?
In Day 5, weโll cover if, else, and elif statements โ how Python makes decisions.
๐ฌ Your Turn!
- Comment below: Whatโs your favorite operator so far
- Share this post if it helped you understand Python better.