Python में if, elif और else
Conditions program को तय करने देती हैं कि क्या करना है: Python हर if या elif को क्रम से check करता है और पहली True condition वाला block चलाता है।
में if, elif और else क्या है?
Conditions program को तय करने देती हैं कि क्या करना है: Python हर if या elif को क्रम से check करता है और पहली True condition वाला block चलाता है।
if, elif और else से decisions लें, और and, or और not से conditions जोड़ें।
इसका उपयोग कब करें?
- चलने योग्य code से Python syntax सीखें।
- बड़ी programming समस्याओं से पहले मजबूत आधार बनाएँ।
- स्थानीय setup के बिना किसी विचार को तुरंत जाँचें।
देखें यह कैसे काम करता है
अंक बदलने के लिए स्लाइडर खिसकाएँ। फ़्लोचार्ट में वह रास्ता चमकता है जिस पर प्रोग्राम चलता है, और कोड की संबंधित पंक्ति हाइलाइट होती है।
उदाहरण कोड
temperature = 23
# if / elif / else picks the first branch whose condition is True.
if temperature >= 30:
print("It's hot")
elif temperature >= 20:
print("It's warm")
elif temperature >= 10:
print("It's cool")
else:
print("It's cold")
# and, or, not combine conditions.
age = 17
has_ticket = True
if age >= 18 and has_ticket:
print("You can enter")
elif not has_ticket:
print("Please buy a ticket")
else:
print("You need an adult with you")
# A conditional expression chooses between two values on one line.
number = 7
kind = "even" if number % 2 == 0 else "odd"
print(number, "is", kind)
अपेक्षित आउटपुट
It's warm
You need an adult with you
7 is odd
यह कैसे काम करता है
Indentation बताती है कि कौन-सी lines किस branch का हिस्सा हैं। and, or और not बड़े conditions बनाते हैं, और value_if_true if condition else value_if_false एक line में value चुनता है।
मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।
अभ्यास के कार्य
बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।
- Code चलाने से पहले output का अनुमान लगाएँ।
- खाली और गलत input संभालें।
- Logic को function में रखें और tests जोड़ें।
चैलेंज से अभ्यास करें
इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।
- Letter Grade Turn a test score into a letter grade. आसान
- Largest of Three Pick the largest of three numbers without a built-in max. आसान
- Ticket Price by Age Choose a cinema ticket price from the visitor's age. आसान