SortedCoding: post #560 — TG.ME

Now, let's move to the next topic in Python Programming Roadmap:

Python Control Flow Part 1: if, elif, else 🧠💻

What is Control Flow?
👉 Your code makes decisions
👉 Runs only when conditions are met

* Each condition is True or False
* Python checks from top to bottom

🔹 Basic if statement
python
age = 20
if age >= 18:
print("You are eligible to vote")

▶️ Checks if age is 18 or more. Prints "You are eligible to vote"

🔹 if-else example
python
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")

▶️ Age is 16, so it prints "Not eligible"

🔹 elif for multiple conditions
python
marks = 72
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")

▶️ Marks = 72, so it matches >= 60 and prints "Grade C"

🔹 Comparison Operators
python
a = 10
b = 20
if a != b:
print("Values are different")

▶️ Since 10 ≠ 20, it prints "Values are different"

🔹 Logical Operators
python
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")

▶️ Both conditions are True → prints "Entry allowed"

⚠️ Common Mistakes:
* Using = instead of ==
* Bad indentation
* Comparing incompatible data types

📌 Mini Project – Age Category Checker
python
age = int(input("Enter age: "))

if age < 13:
print("Child")
elif age <= 19:
print("Teen")
else:
print("Adult")

▶️ Takes age as input and prints the category


📝 Practice Tasks:
1. Check if a number is even or odd
2. Check if number is +ve, -ve, or 0
3. Print the larger of two numbers
4. Check if a year is leap year

Practice Task Solutions – Try it yourself first 👇

1️⃣ Check if a number is even or odd
python
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

▶️ % gives remainder. If remainder is 0, it's even.


2️⃣ Check if number is positive, negative, or zero
python
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")

▶️ Uses > and < to check sign of number.


3️⃣ Print the larger of two numbers
python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("Larger number is:", a)
elif b > a:
print("Larger number is:", b)
else:
print("Both are equal")

▶️ Compares a and b and prints the larger one.


4️⃣ Check if a year is leap year
python
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

▶️ Follows leap year rules:
- Divisible by 4
- But not divisible by 100
- Unless also divisible by 400


📅 Daily Rule:
Code 60 mins
Run every example
Change inputs and observe output
❤3
January 11, 2026 557