Python · Chapter 2.3 · Basic level

Decision Power (if, elif, else)

फ़ैसला लेने की शक्ति (if, elif, else)

How does an app decide whether someone is old enough to vote, or which prize to give based on marks? Using if, elif (else-if), and else! Follow Python's 2 Strict Rules:
1. Every if, elif, and else line MUST end with a colon : (two dots).
2. The code belonging inside it MUST have 4 spaces at the front (called Indentation — press Tab!).

कंप्यूटर खुद फ़ैसला कैसे लेता है कि किसे वोट डालने देना है या कितने मार्क्स पर कौन सा इनाम मिलेगा? if (अगर), elif (वरना अगर), और else (वरना) से! बस Python के 2 पक्के नियम याद रखो:
1. if, elif, और else वाली लाइन के अंत में दो बिंदियाँ : लगाना 100% ज़रूरी है!
2. उसके अंदर की लाइन के आगे 4 स्पेस (Tab) की जगह छोड़नी होती है जिसे Indentation कहते हैं!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

if age >= 18:
If condition is True (age is 18 or more), run the indented block below.
अगर शर्त सही है (age 18 या उससे ज़्यादा है), तो नीचे वाला कोड चलाओ।
>=
Greater than or equal to (>=), Less than (<), Equal to (==).
बड़ा या बराबर (>=), छोटा (<), बराबर की जाँच (==)।
: (Colon)
Mandatory punctuation at the end of if, elif, else headers!
if, elif, else वाली लाइन के अंत में लगने वाली ज़रूरी दो बिंदियाँ!
(4 Spaces)
Indentation — shows Python which lines live inside the if block.
Indentation — आगे के 4 स्पेस बताते हैं कि यह लाइन if के अंदर है।
elif ...:
Else If — checked only when the upper if was False.
वरना अगर — जब ऊपर वाला if ग़लत हो तब दूसरी शर्त जाँचता है।
else:
Otherwise — runs when none of the above conditions matched.
वरना — जब ऊपर की कोई भी शर्त सही न हो तब यह चलता है।

Ready example

age = 20

if age >= 18:
    print("Can Vote! Aap vote de sakte hain.")
else:
    print("Too Young! Abhi umar kam hai.")

What you will see: A live Traffic-Signal Flowchart lights up GREEN when True (age = 20) or RED else-path when age = 12!

प्रिव्यू में ट्रैफ़िक सिग्नल फ़्लोचार्ट दिखेगा जो 20 पर हरा (Green) और 12 करने पर लाल रास्ता चमकाएगा!

Line by line

age = 20
Sets age to 20 (try changing 20 to 12 and hit Run to see the Red path!).
age में 20 रखा (इसे 12 बदलकर Run दबाओ तो लाल रास्ता चमकेगा!)।
if age >= 18:
Checks if age is at least 18 — notice the colon : at the end!
जाँचता है कि क्या age 18 या ज़्यादा है — अंत में : ध्यान से देखो!
print("Can Vote!...")
Indented by 4 spaces — runs when condition is True.
4 स्पेस आगे बढ़कर लिखा — शर्त सही होने पर छपता है।
else:
Fallback branch when age is less than 18.
अगर उम्र 18 से कम हो तो else: वाला हिस्सा चलता है।

Common mistake

✗ Wrong

if age >= 18
print("Vote")

✓ Right

if age >= 18:
    print("Vote")

Never forget the colon : at the end of if/else AND the 4-space indentation on the next line!

if और else के अंत में दो बिंदियाँ : लगाना और अगली लाइन में 4 स्पेस देना कभी न भूलें!

Your task

Create age = 20 (or >= 18), write if age >= 18: to print("Can Vote"), and else: to print("Too Young")!

age = 20 बनाओ, if age >= 18: में print("Can Vote") लिखो, और else: में print("Too Young") लिखो!

  1. Set age = 20.
    age = 20 लिखो।
  2. Write if age >= 18: (don't forget :) and indent 4 spaces to print("Can Vote").
    if age >= 18: लिखो और 4 स्पेस देकर print("Can Vote") लिखो।
  3. Write else: and indent 4 spaces to print("Too Young"), then Run!
    else: लिखो और 4 स्पेस देकर print("Too Young") लिखकर Run दबाओ!
Starter code for the task
age = 20

# TODO 1: if age >= 18: likho (aakhir mein colon : zaroor)
# TODO 2: uske neeche 4 spaces dekar print("Can Vote")
# TODO 3: else: likho aur uske neeche 4 spaces dekar print("Too Young")

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

In Python, = assigns a value. Which operator checks if two values are EQUAL?

Python में = वैल्यू रखता है, तो दो चीज़ों की बराबरी जाँचने के लिए क्या लगता है?

  1. =
  2. ===
  3. ==
  4. :=
Show the answer

Answer: C (==)

Double equals == compares two values (marks == 100).

डबल बराबर == तुलना करके बताता है कि दोनों बराबर हैं या नहीं।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.