TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2496 7.33K
โœ… 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
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
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
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
a = 10  
b = 20
if a != b:
print("Values are different")

โ–ถ๏ธ Since 10 โ‰  20, it prints "Values are different"

๐Ÿ”น Logical Operators
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
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
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
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
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
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

๐Ÿ’ฌ Tap โค๏ธ if this helped you!

Python Programming Roadmap: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/2312
  • โค 15
More from @sqlspecialist
  1. Oct 12, 20268๏ธโƒฃ You receive a dataset with inconsistent date formats. How would you clean it? Sample Aโ€ฆ
  2. Oct 12, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 7 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  4. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  5. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  6. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’