Conditional Statements (if, elif, else)
Har program ko eventually value ke hisaab se alag alag cheezein karni padti hain. Python isके liye if, elif ("else if" ka short form), aur else use karta hai — aur curly braces ke bajaye, yeh indentation use karta hai yeh batane ke liye ki har block ke andar kya aata hai.
age = 20
if age >= 18:
print("You are an adult")
else:
print("You are a minor")You are an adult
Woh indentation sirf ek style choice nahi hai — Python isi se jaanta hai ki if block ke andar kya aata hai. Isse galat karoge (tabs aur spaces mix kar doge, ya inconsistently indent karoge) to ek real IndentationError milega, koi chup chaap bug nahi.
elif se conditions ko chain karna
score = 75
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")C
Python har condition ko upar se neeche check karta hai aur jo pehli condition true milti hai wahin ruk jaata hai — score >= 70 else tak pahunchne se pehle hi match ho jaata hai, isliye "C" print hota hai aur uske baad sab skip ho jaata hai.
Aage: for loops — collection ke har item par code ko repeat karna.
Practice Exercise
age = 16 set karo. Agar age 18 se kam hai to "Minor" print karo, warna "Adult" print karo.