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