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 कहते हैं!
Words and symbols in this chapter
if age >= 18:- If condition is True (
ageis 18 or more), run the indented block below. - अगर शर्त सही है (
age18 या उससे ज़्यादा है), तो नीचे वाला कोड चलाओ। >=- Greater than or equal to (
>=), Less than (<), Equal to (==). - बड़ा या बराबर (
>=), छोटा (<), बराबर की जाँच (==)। : (Colon)- Mandatory punctuation at the end of
if,elif,elseheaders! if,elif,elseवाली लाइन के अंत में लगने वाली ज़रूरी दो बिंदियाँ!(4 Spaces)- Indentation — shows Python which lines live inside the
ifblock. - Indentation — आगे के 4 स्पेस बताते हैं कि यह लाइन
ifके अंदर है। elif ...:- Else If — checked only when the upper
ifwas 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
ageto20(try changing20to12and hit Run to see the Red path!). ageमें20रखा (इसे12बदलकर Run दबाओ तो लाल रास्ता चमकेगा!)।if age >= 18:- Checks if
ageis at least 18 — notice the colon:at the end! - जाँचता है कि क्या
age18 या ज़्यादा है — अंत में:ध्यान से देखो! print("Can Vote!...")- Indented by 4 spaces — runs when condition is True.
- 4 स्पेस आगे बढ़कर लिखा — शर्त सही होने पर छपता है।
else:- Fallback branch when
ageis 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") लिखो!
- Set
age = 20.age = 20लिखो। - Write
if age >= 18:(don't forget:) and indent 4 spaces toprint("Can Vote").if age >= 18:लिखो और 4 स्पेस देकरprint("Can Vote")लिखो। - Write
else:and indent 4 spaces toprint("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
if,elifandelselet the program choose a path.- Each of these lines ends with a colon
:. - The code inside must be indented by 4 spaces.
if,elifऔरelseसे प्रोग्राम रास्ता चुनता है।- इन हर लाइन के आख़िर में colon
:लगता है। - अंदर का कोड 4 स्पेस आगे (indent) होना चाहिए।
Quick quiz
In Python, = assigns a value. Which operator checks if two values are EQUAL?
Python में = वैल्यू रखता है, तो दो चीज़ों की बराबरी जाँचने के लिए क्या लगता है?
Show the answer
Answer: C (==)
Double equals == compares two values (marks == 100).
डबल बराबर == तुलना करके बताता है कि दोनों बराबर हैं या नहीं।
Practice problems for this chapter
- Fix the Grade Machine (if / elif / else)
ग्रेड मशीन ठीक करो (if / elif / else)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.