प्रोग्रामर्स के लिए पायथन कंट्रोल फ्लो टूल्स | पायथन डेवलपर
पायथन कुछ ट्विस्ट के साथ अन्य प्रोग्रामिंग भाषाओं के समान सामान्य नियंत्रण कथनों का उपयोग करता है।
if statements
"If" एक अधिक सामान्य और बहुमुखी बयान है और यह हर प्रोग्रामिंग भाषा में पाया जाता है। यह एक बहुत ही महत्वपूर्ण कथन है क्योंकि यह ज्यादातर प्रोग्रामर द्वारा उपयोग किया जाता है। इसे शुरुआती से लेकर प्रो तक हर प्रोग्रामर आसानी से समझ सकता है।
x = int(input("Enter any number: "))
if x > 500:
print("The input number is more than 500")
elif x > 200 and x < 500:
print("The input number is more than 200 and less then 500")
elif x > 100 and x < 200:
print("The input number is more than 100 and less then 200")
else:
print("The input number is less then 200")
'''output:
Enter any number: 236
The input number is more than 200 and less then 500'''
कथन में शून्य या अधिक "elif" भाग हो सकते हैं, "else" यहाँ वैकल्पिक है, इसलिए यदि आपके पास आवश्यकताएँ नहीं हैं तो आप इसे छोड़ सकते हैं। अत्यधिक इंडेंटेशन से बचने के लिए कीवर्ड "elif" else if स्टेटमेंट का संक्षिप्त नाम है।
"elif" अनुक्रम और "match case" के बीच विरोध हो सकता है।. "switch case" के करीब कौन है? हालांकि आधिकारिक दस्तावेज में कहा गया है कि एक "elif...elif...elif..." अनुक्रम Python में एक स्विच स्टेटमेंट के लिए एक विकल्प है क्योंकि अन्य प्रोग्रामिंग भाषाओं की तरह Python में कोई स्विच केस नहीं है, लेकिन "match case" मेरे विचार में "switch case" के करीब है। मैं आपको दिखाऊंगा कि मैं ऐसा क्यों सोचता हूं।
हम एक लोकप्रिय भाषा जावास्क्रिप्ट चुनते हैं, यहाँ "switch case" कैसा दिखता है, अन्य प्रोग्रामिंग भाषाओं में भी एक समान पैटर्न होता है।
switch (expression) {
case value1:
// code to be executed if expression === value1
break;
case value2:
// code to be executed if expression === value2
break;
default:
// code to be executed if expression doesn't match any of the above cases
break;
}
अब बात आती है Python में "match case" की, जिसका स्टेटमेंट कुछ इस तरह दिखता है।
match (expression) {
case value1:
# code to be executed if expression === value1
case value2:
# code to be executed if expression === value2
case _:
# code to be executed if expression doesn't match any of the above cases
}
यहां आप कह सकते हैं कि "मैच केस" "स्विच केस" के समान है, हम बाद में "match" स्टेटमेंट के बारे में अधिक बताएंगे।
for statements
पायथन में "फॉर" लूप को थोड़े अलग तरीके से लिखा गया है, जैसा कि हम जानते हैं कि अधिकांश प्रोग्रामिंग भाषाओं में संख्याओं की अंकगणितीय प्रगति की जाती है, लेकिन पायथन के "फॉर" लूप में ऐसा नहीं है। यदि आपको इसकी आवश्यकता है तो आपको "for" लूप में "range()" फ़ंक्शन का उपयोग करना होगा, हम इसके बारे में बाद में विस्तार से बात करेंगे। आमतौर पर, पायथन के "फॉर" लूप में, वस्तुओं का एक अनुक्रम (सूची या स्ट्रिंग) उस क्रम में बार-बार दोहराया जाता है जिसमें वे अनुक्रम में होते हैं।
fruits = ["Apple", "Banana", "Orange", "Grapes"]
for i in fruits:
print(f"Fruit name: {i}")
'''output:
Fruit name: Apple
Fruit name: Banana
Fruit name: Orange
Fruit name: Grapes'''
The range() function
यदि आप अंकगणितीय प्रगति के साथ लूप को बार-बार दोहराना चाहते हैं, तो यह बिल्ट-इन range() फ़ंक्शन बहुत उपयोगी है। इस range() फंक्शन को आप अपनी जरूरत के हिसाब से कई जगह इस्तेमाल कर सकते हैं। range() के माध्यम से यह संभव है कि यह किसी भी शुरुआती संख्या से दी गई संख्या तक जा सकता है वह भी निश्चित वृद्धि के साथ जिसे हम कदम भी कह सकते हैं। आइए देखें कि range() फ़ंक्शन कैसे काम करता है।
# To iterate over the indices of a sequence, you can combine range() and len() as follows:
fruits = ["Apple", "Banana", "Orange", "Grapes"]
for i in range(len(fruits)):
print(f"The name of the fruit at index {i} is: {fruits[i]}")
# list with range()
print(list(range(1,9)))
# step in range()
print(list(range(10,99, 15)))
'''output:
The name of the fruit at index 0 is: Apple
The name of the fruit at index 1 is: Banana
The name of the fruit at index 2 is: Orange
The name of the fruit at index 3 is: Grapes
[1, 2, 3, 4, 5, 6, 7, 8]
[10, 25, 40, 55, 70, 85]'''
break, continue statements and else clause in loops
यहाँ ब्रेक स्टेटमेंट का कार्य "C" प्रोग्रामिंग के समान है, जब हम इस ब्रेक स्टेटमेंट का उपयोग करते हैं तो यह लूप के इनर सर्कल से बाहर आता है। लूप स्टेटमेंट में एक "else" क्लॉज हो सकता है जिसका काम "for" लूप पूरा होने पर "else" कारण पर कूदना है, लेकिन अगर "ब्रेक" स्टेटमेंट का उपयोग किया जाता है तो "else" क्लॉज निष्क्रिय हो जाएगा। आइए देखें कैसे।
# break statement
for i in range(1, 10):
for i1 in range(1, i):
if (i % 2 == 0):
print(f"The number {i} is an even number.")
break
else:
print(f"The number {i} is an odd number.")
'''output:
The number 1 is an odd number.
The number 2 is an even number.
The number 3 is an odd number.
The number 4 is an even number.
The number 5 is an odd number.
The number 6 is an even number.
The number 7 is an odd number.
The number 8 is an even number.
The number 9 is an odd number.'''
यहाँ "Continue Statement" "C" प्रोग्रामिंग से आता है, इसका काम लूप को चालू रखना है।
# continue statement
for i in range(1, 10):
if (i % 2 == 0):
print(f"The number {i} is an even number.")
continue
else:
print(f"The number {i} is an odd number.")
'''output:
The number 1 is an odd number.
The number 2 is an even number.
The number 3 is an odd number.
The number 4 is an even number.
The number 5 is an odd number.
The number 6 is an even number.
The number 7 is an odd number.
The number 8 is an even number.
The number 9 is an odd number.'''
pass statements
हम "पास" स्टेटमेंट का उपयोग तब करते हैं जब हमें किसी समय किसी स्टेटमेंट की आवश्यकता होती है, लेकिन हम उस स्टेटमेंट के ब्लॉक को अभी निष्पादित नहीं करना चाहते हैं, तब हम "पास" स्टेटमेंट का उपयोग करते हैं। यह करना बहुत आसान है, आइए देखते हैं कैसे:
if True:
pass
class EmptyClass:
pass
def emptyFun(*args):
pass
for i in [0, 1, 2]:
pass
match statements
"match" स्टेटमेंट में, हम एक एक्सप्रेशन लेते हैं और इसकी तुलना एक या अधिक केस ब्लॉक से करते हैं। जिस केस ब्लॉक से अभिव्यक्ति मिलती है, उसे पहले निष्पादित किया जाएगा, और बाकी सभी को छोड़ देगा। "match" कथन कुछ प्रोग्रामिंग भाषाओं जैसे "C, Java, या JavaScript" में "switch" कथन के समान है, हालांकि यह "Rust और Haskell" से अधिक मेल खाता है।
def getError(errCode):
match errCode:
case 400:
print("Bad Request")
case 401 | 402 | 403:
print("Unauthorized, Payment Required, Forbidden")
case 404:
print("Not Found")
case 407:
print("Proxy Authentication Required")
case _:
print("Not Allowed")
getError(404)
# output: Not Found
यदि हम डेटा की संरचना कर रहे हैं, तो वर्ग के नाम के बाद, हम तर्क सूची में चर के प्रकार भी निर्दिष्ट कर सकते हैं और इन विशेषताओं को चर के रूप में भी उपयोग कर सकते हैं।
Functions
Functions किसी भी प्रोग्रामिंग लैंग्वेज में बहुत काम की चीज होती है, यह आपको लगभग हर प्रोग्रामिंग लैंग्वेज में मिल जाएगी, बस लिखने के तरीके में बदलाव देखा जा सकता है। पायथन में एक फ़ंक्शन लिखने के लिए, "def" कीवर्ड का उपयोग किया जाता है, इसके बाद फ़ंक्शन का नाम और फिर कोष्ठक। हम कोष्ठक में पैरामीटर का भी उपयोग कर सकते हैं, यह वैकल्पिक है।
def tableOf(n):
print(f"Table of {n}")
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
tableOf(17)
'''output:
Table of 17
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170'''
Default Argument Values
आपको कितने तर्कों की आवश्यकता है, इस पर निर्भर करते हुए फ़ंक्शन में एक या अधिक तर्क हो सकते हैं।
def tableOf(n, start, end):
print(f"Table of {n}")
for i in range(start, end):
print(f"{n} x {i} = {n*i}")
tableOf(13, 1, 5)
'''output:
Table of 13
13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65'''
यहाँ पर हमने Function में बहुत से Argument का इस्तमाल किया है जिसमे “n” का एक Table लिखा होता है जो 1 से शुरू होता है और 5 पर खत्म होता है।
Keyword Arguments
हम कीवर्ड तर्क के माध्यम से फ़ंक्शन को भी कॉल कर सकते हैं, देखते हैं कि यह कैसे किया जाता है।
def animals(name, group = "mammals", color = "N/A", description = "N/A"):
print(f"The {name} belongs to the group of {group} and its color is {color}, described as {description}.")
animals("Tiger")
animals("Black-necked cobra", group="reptiles")
animals(name="Parrot", color="vividly coloured, and some are multi-coloured", group="birds", description="strong, curved bill, an upright stance, strong legs, and clawed zygodactyl feet")
'''output:
The Tiger belongs to the group of mammals and its color is N/A, described as N/A.
The Black-necked cobra belongs to the group of reptiles and its color is N/A, described as N/A.
The Parrot belongs to the group of birds and its color is vividly coloured, and some are multi-coloured, described as strong, curved bill, an upright stance, strong legs, and clawed zygodactyl feet.'''
औपचारिक पैरामीटर "** args" मौजूद होने पर यह शब्दकोश प्राप्त करेगा, जिसका अर्थ है कि यह कीवर्ड और तर्क जोड़ी लेगा, इससे पहले हमें औपचारिक पैरामीटर के लिए "* args" का उपयोग करना होगा जो औपचारिक पैरामीटर सूची से परे स्थितीय तर्कों से युक्त टपल प्राप्त करता है। . "** args" से पहले "*args" का उपयोग करना महत्वपूर्ण है।
def restaurant(beverage, *arguments, **keyword):
print(f"Do you have {beverage}?")
print(f"Sorry sir, we don't have {beverage}.")
for args in arguments:
print(args,)
print("-" * 40)
for kwrd in keyword:
print(f"{kwrd} : {keyword[kwrd]}")
restaurant("pizza", "If you don't have the pizza, please get my order for the burger", " with extra onions", " and extra cheese.", waitre = "Would you like to have something else?", costumer = "No, thank you!", waitress = "Do you like mineral water or tap water?", client = "Tap water please.")
'''output:
Do you have pizza?
Sorry sir, we don't have pizza.
If you don't have the pizza, please get my order for the burger
with extra onions
and extra cheese.
----------------------------------------
waitre : Would you like to have something else?
costumer : No, thank you!
waitress : Do you like mineral water or tap water?
client : Tap water please.'''
Special Parameters
आम तौर पर, पायथन फ़ंक्शन के तर्क या तो स्थिति या स्पष्ट रूप से कीवर्ड द्वारा पारित किए जाते हैं। डेवलपर की सुविधा और कार्यक्रम के प्रदर्शन के लिए, यदि हम निर्दिष्ट करते हैं कि कौन सा स्थितीय तर्क है और कौन सा कीवर्ड तर्क है, तो यह प्रोग्राम और डेवलपर दोनों के लिए बहुत अच्छा होगा।
एक फ़ंक्शन परिभाषा इस तरह दिख सकती है:
यहां "/" और "*" वैकल्पिक हैं, यदि यह इंगित करने के लिए मौजूद हैं कि कौन से स्थितीय-मात्र तर्क, स्थिति-या-कीवर्ड तर्क, और केवल-कीवर्ड तर्क हैं। हम कीवर्ड पैरामीटर को नामित पैरामीटर भी कहते हैं।
Positional-or-Keyword Arguments
यदि "/" और "*" फ़ंक्शन परिभाषा में मौजूद नहीं हैं, तो इसका अर्थ है एक स्थितीय-या-कीवर्ड तर्क जो स्थितीय और कीवर्ड दोनों तर्कों को स्वीकार करेगा।
Positional-Only Parameters
positional-only पैरामीटर्स के लिए हम फंक्शन डेफिनिशन में "/" का उपयोग करेंगे, "/" से पहले जो भी पैरामीटर मौजूद हैं, वे positional-only पैरामीटर होंगे। "/" का अनुसरण करने वाला कोई भी पैरामीटर या तो स्थितीय-या-कीवर्ड या केवल-कीवर्ड हो सकता है। यदि "/" फ़ंक्शन परिभाषा में मौजूद नहीं है, तो यह केवल स्थितीय पैरामीटर नहीं हो सकता है।
Keyword-Only Arguments
"*" फ़ंक्शन परिभाषा में कीवर्ड-ओनली पैरामीटर के लिए उपयोग किया जाता है, और बाद के सभी पैरामीटर केवल-कीवर्ड पैरामीटर हैं।
स्थितीय-या-कीवर्ड, केवल-स्थितीय, केवल-कीवर्ड तर्क फ़ंक्शन उदाहरण
def stnd_args(args):
print(args)
def pos_only_args(args, /):
print(args)
def kwrd_only_args(*, args):
print(args)
def mixed_args(pos_only, /, pos_or_kwrd, *, kwrd_only):
print(pos_only, pos_or_kwrd, kwrd_only)
पहली फ़ंक्शन परिभाषा stnd_args सबसे आम है और आप इसे अधिकांश स्थानों पर बिना किसी प्रतिबंध के देखेंगे जहाँ आप स्थितीय पैरामीटर या कीवर्ड पैरामीटर या दोनों का उपयोग कर सकते हैं।
stnd_args(23)
stnd_args(args = 23)
'''output:
23
23'''
दूसरी फ़ंक्शन परिभाषा में pos_only_args में "/" का उपयोग किया गया है जिसका अर्थ है कि यह केवल स्थितीय पैरामीटर लेगा।
pos_only_args(6)
pos_only_args(args = 6)
'''output:
6
Traceback (most recent call last):
File "d:\exercise\python\python-control-flow-tools.py", line 124, in <module>
pos_only_args(args = 6)
TypeError: pos_only_args() got some positional-only arguments passed as keyword arguments: 'args''''
तीसरे फंक्शन डेफिनिशन में kwrd_only_args में "*" का प्रयोग किया गया है जो बताता है कि इसमें केवल कीवर्ड पैरामीटर का उपयोग किया जाएगा, यदि इसके स्थान पर पोजीशनल पैरामीटर का उपयोग किया जायेगा है तो इंटरप्रेटर एरर देगा।
kwrd_only_args(39)
kwrd_only_args(args = 39)
'''output:
Traceback (most recent call last):
File "d:\exercise\python\python-control-flow-tools.py", line 125, in <module>
kwrd_only_args(39)
TypeError: kwrd_only_args() takes 0 positional arguments but 1 was given
39'''
अंतिम फ़ंक्शन परिभाषा में हम मिश्रित_आर्ग में सभी पैरामीटर का उपयोग करेंगे।
mixed_args(29, 32, 64)
mixed_args(29, 32, kwrd_only = 64)
mixed_args(29, pos_or_kwrd = 32, kwrd_only = 64)
'''output:
Traceback (most recent call last):
File "d:\exercise\python\python-control-flow-tools.py", line 127, in <module>
mixed_args(29, 32, 64)
TypeError: mixed_args() takes 2 positional arguments but 3 were given
29 32 64
29 32 64'''
अब फंक्शन डेफिनिशन पर आ रहे हैं जिसमें 'नाम' पैरामीटर है और **kwrd जिसमें 'नाम' कुंजी का भी उपयोग किया जाता है, ऐसे मामले में दुभाषिया त्रुटि देगा।
def foo(name, **kwrd):
return 'name' in kwrd
print(foo(7, **{'name' : 27}))
'''output:
Traceback (most recent call last):
File "d:\exercise\python\python-control-flow-tools.py", line 133, in <module>
print(foo(7, **{'name' : 27}))
TypeError: foo() got multiple values for argument 'name''''
स्थितीय-केवल तर्क "/" का उपयोग करने के बाद यह नाम को स्थितिगत तर्कों के रूप में और 'नाम' को कीवर्ड की कुंजी के रूप में लेगा।
def foo(name, /, **kwrd):
return 'name' in kwrd
print(foo(5, **{'name' : 39}))
# output: True
यहाँ हमने python में इस्तेमाल होने वाले बहुत से control Flow tools के बारे में पढ़ा और ये भी जाना की कैसे इसे इस्तेमाल करना है। कोई भी प्रोग्रामिंग लैंग्वेज तब तक कठिन लगती है जब तक हम उसे सरल भाषा में समझने की कोशिश नहीं करते हैं, अगर आपको सरल भाषा में उन सभी चीजों को समझाया जाए तो आप इसे बिना किसी समस्या के आसानी से सीख सकते हैं। यहाँ पर मैं आपको Python Programming से सम्बंधित सभी जानकारी सरल भाषा में देने की पूरी कोशिश की है।
यदि आपको यह ब्लॉग उपयोगी लगता है, तो कृपया मुझे एक कॉफी खरीदने पर विचार करें। 