शर

शर्तें में Python

94 अभ्यास

शर्तें के बारे में

Python में if, elif ('else and if' का छोटा रूप) और else स्टेटमेंट का इस्तेमाल प्रोग्राम में फैसले लेने और चलने के क्रम को नियंत्रित करने के लिए किया जाता है। दूसरी कई प्रोग्रामिंग भाषाओं के उलट, Python के 3.9 और उससे पुराने वर्शन में कोई औपचारिक केस-स्विच स्टेटमेंट नहीं मिलता। इसकी जगह मिलते-जुलते काम के लिए कई elif स्टेटमेंट इस्तेमाल किए जाते हैं।

Python 3.10 में pattern matching नाम का एक अलग तरह का केस-स्विच स्टेटमेंट आया है, जिसे आगे किसी दूसरे कॉन्सेप्ट में अलग से समझाया जाएगा।

कंडीशनल स्टेटमेंट ऐसे एक्सप्रेशन इस्तेमाल करते हैं जिनका परिणाम True या False होना चाहिए। यह या तो सीधे bool लौटाकर होता है, या "ट्रुथी" या "फल्सी" जाँचकर।

x = 10
y = 5

# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
    print("x is greater than y")
...
>>> x is greater than y

if के साथ जोड़े जाने पर एक वैकल्पिक else कोड ब्लॉक तब चलता है जब मूल if शर्त का परिणाम False निकलता है:

x = 5
y = 10

# The comparison '>' here returns the bool False,
# so the 'else' block is executed instead of the 'if' block.
if x > y:
    print("x is greater than y")
else:
    print("y is greater than x")
...
>>> y is greater than x

elif की मदद से शर्तों की कई जाँचें की जा सकती हैं और कई शाखाएँ बनाई जा सकती हैं।

x = 5
y = 10
z = 20

# The elif statement allows for the checking of more conditions.
if x > y > z:
    print("x is greater than y and z")
elif y > x > z:
    print("y is greater than x and z")
else:
    print("z is greater than x and y")
...
>>> z is greater than x and y

बूलियन ऑपरेशन और तुलना को कंडीशनल स्टेटमेंट के साथ जोड़कर ज़्यादा जटिल जाँचें की जा सकती हैं:

>>> def classic_fizzbuzz(number):
...     if number % 3 == 0 and number % 5 == 0:
...         say = 'FizzBuzz!'
...     elif number % 5 == 0:
...         say = 'Buzz!'
...     elif number % 3 == 0:
...         say = 'Fizz!'
...     else:
...         say = str(number)
...
...     return say
        
>>> classic_fizzbuzz(15)
'FizzBuzz!'

>>> classic_fizzbuzz(13)
'13'

वैकल्पिक तरीके के तौर पर ऊपर वाला उदाहरण सिर्फ if स्टेटमेंट और returns इस्तेमाल करके दोबारा लिखा जा सकता है। लेकिन इस तरह लिखने पर यह बात छिप सकती है कि ये शर्तें एक-दूसरे को बाहर रखने वाली हैं, और आगे चलकर इससे बग या मेंटेनेंस की दिक्कतें पैदा हो सकती हैं।

>>> def classic_fizzbuzz(number):
...     if number % 3 == 0 and number % 5 == 0:
...         return 'FizzBuzz!'
...     if number % 5 == 0:
...         return 'Buzz!'
...     if number % 3 == 0:
...         return 'Fizz!'
...
...     return str(number)

>>> classic_fizzbuzz(15)
'FizzBuzz!'

>>> classic_fizzbuzz(13)
'13'

कंडीशनल स्टेटमेंट नेस्टेड भी हो सकते हैं।

>>> def driving_status(driver_age, test_score):
...     if test_score >= 80:
...         if 18 > driver_age >= 16:
...             status = "Student driver, needs supervision."
...         elif driver_age == 18:
...             status = "Permitted driver, on probation."
...         elif driver_age > 18:
...             status = "Fully licensed driver."
...     else:
...         status = "Unlicensed!"
...
...     return status


>>> driving_status(63, 78)
'Unlicensed!'

>>> driving_status(16, 81)
'Student driver, needs supervision.'

>>> driving_status(23, 80)
'Fully licensed driver.'

कंडीशनल एक्सप्रेशन या "टर्नरी ऑपरेटर"

Python में ? जैसा कोई अलग टर्नरी ऑपरेटर नहीं है, लेकिन एक ही लाइन में conditional expressions लिखना मुमकिन है। इनका रूप <value if True> if <conditional test> else <value if False> होता है। चूँकि ये एक्सप्रेशन पढ़ने में भारी पड़ सकते हैं, इसलिए इस एक-लाइन वाले रूप का इस्तेमाल तभी कीजिए जब इससे कोड छोटा हो और पढ़ने में आसानी बढ़े।

>>> def just_the_buzz(number):
...     return 'Buzz!' if number % 5 == 0 else str(number)
    
>>> just_the_buzz(15)
'Buzz!'

>>> just_the_buzz(7)
'7'

ट्रुथी और फल्सी

Python में किसी भी ऑब्जेक्ट की ट्रुथ वैल्यू जाँची जा सकती है, इसलिए उसे किसी कंडीशनल, तुलना या बूलियन ऑपरेशन के साथ इस्तेमाल किया जा सकता है। जो ऑब्जेक्ट इस तरह जाँचे जाते हैं उन्हें "ट्रुथी" या "फल्सी" माना जाता है, और उनका इस्तेमाल boolean context में होता है।

>>> def truthy_test(thing):
...     if thing:
...         print('This is Truthy.')
...     else:
...         print("Nope. It's Falsy.")


# Empty container objects are considered Falsy.
>>> truthy_test([])
"Nope. It's Falsy."

>>> truthy_test(['bear', 'pig', 'giraffe'])
'This is Truthy.'

# Empty strings are considered Falsy.
>>> truthy_test('')
"Nope. It's Falsy."

>>> truthy_test('yes')
'This is Truthy.'

# 0 is also considered Falsy.
>>> truthy_test(0)
"Nope. It's Falsy."
GitHub के ज़रिए संपादित करें यह लिंक नई विंडो या टैब में खुलता है।

शर्तें सीखिए

अभ्यास लॉक है

शर्तें पर अभ्यास करने के लिए 4 और अभ्यास अनलॉक कीजिए