Basics
if-elif-else
Indentation-Based Blocks
💡 Python ka if-else ek recipe ke steps jaisa hai jaha indentation (spacing) hi batata hai kaunsa step kis section ka hissa hai — koi curly braces {} nahi chahiye, spacing khud structure define karti hai.
Python if, elif (C ke "else if" ka short form), else use karta hai. SABSE BADA farq: Python mein curly braces {} nahi hote block define karne ke liye — indentation (usually 4 spaces) hi decide karti hai kaunsa code kis block ka part hai. Galat indentation = syntax error.
marks = 85
if marks >= 90:
print("A grade")
elif marks >= 75:
print("B grade") # ye print hoga
else:
print("C grade")
# Ternary jaisa (conditional expression):
grade = "Pass" if marks >= 75 else "Fail"
print(grade) # "Pass"🔀
Python ka if-else ek recipe ke steps jaisa hai jaha indentation (spacing) hi batata hai kaunsa step kis section ka hissa hai — koi curly braces {} nahi chahiye, spacing khud structure define karti hai.
1 / 2
⚡ झट से Recap
- elif = "else if" ka short form
- Curly braces nahi — indentation hi block define karti hai
- Conditional expression: value_if_true if condition else value_if_false
इस page में (1 subtopics)
Python mein har value ek implicit boolean context mein True ya False mein convert ho sakti hai — "falsy" values: 0, 0.0, "" (empty string), [] (empty list), {} (empty dict), None. Baaki sab kuch "truthy" hai.
name = ""
if name: # empty string = falsy
print("Name diya hai")
else:
print("Name khaali hai") # ye chalega
numbers = [1, 2, 3]
if numbers: # non-empty list = truthy
print("List mein data hai") # ye chalegaTip: if len(list) > 0: likhne ke bajaye, direct if list: likhna zyada Pythonic hai — empty list automatically falsy hai.