Python में लूप के दो तरीके हैं।
while लूप अनिश्चित (बिना गिने) इटरेशन के लिए हैं, और for लूप निश्चित (गिने हुए) इटरेशन के लिए।
कीवर्ड break, continue और else लूप के व्यवहार को अपने हिसाब से बदलने में मदद करते हैं।
range() और enumerate() लूप गिनने और इंडेक्स करने में मदद करते हैं।
while लूपwhile लूप तब तक चलते रहते हैं जब तक कि loop expression या "टेस्ट" का किसी boolean context में परिणाम True रहता है, और जब उसका परिणाम False हो जाता है तो वे रुक जाते हैं:
# Lists are considered "truthy" in a boolean context if they
# contain one or more values, and "falsy" if they are empty.
>>> placeholders = ["spam", "ham", "eggs", "green_spam", "green_ham", "green_eggs"]
>>> while placeholders:
... print(placeholders.pop(0))
...
'spam'
'ham'
'eggs'
'green_spam'
'green_ham'
'green_eggs'
for लूपPython में बुनियादी for loop को for each कहना ज़्यादा ठीक रहता है, जो किसी भी इटरेबल ऑब्जेक्ट की वैल्यू पर एक-एक करके चलता है और तब रुक जाता है जब next() को कॉल करने पर कोई वैल्यू नहीं लौटती:
>>> word_list = ["bird", "chicken", "barrel", "bongo"]
>>> for word in word_list:
... if word.startswith("b"):
... print(f"{word.title()} starts with a B.")
... else:
... print(f"{word.title()} doesn't start with a B.")
...
'Bird starts with a B.'
'Chicken doesn\'t start with a B.'
'Barrel starts with a B.'
'Bongo starts with a B.'
जब कोई खास iterable नहीं दिया गया हो, तो लूप काउंटर के तौर पर range() नाम का खास सीक्वेंस इस्तेमाल किया जाता है।
range() को एक int देना ज़रूरी है जिससे पहले सीक्वेंस stop हो जाए, और यह चाहें तो start और step पैरामीटर भी ले सकता है।
अगर start संख्या नहीं दी गई हो, तो सीक्वेंस 0 से शुरू होगा।
range() ऑब्जेक्ट लेज़ी होते हैं (वैल्यू माँगने पर ही बनाई जाती हैं), इन पर सभी सामान्य सीक्वेंस ऑपरेशन लागू होते हैं, और सीक्वेंस चाहे कितना भी लंबा हो, ये तय मात्रा में ही मेमोरी लेते हैं।
# Here we use range to produce some numbers, rather than creating a list of them in memory.
# The values will start with 1 and stop *before* 7
>>> for number in range(1, 7):
... if number % 2 == 0:
... print(f"{number} is even.")
... else:
... print(f"{number} is odd.")
'1 is odd.'
'2 is even.'
'3 is odd.'
'4 is even.'
'5 is odd.'
'6 is even.'
# range() can also take a *step* parameter.
# Here we use range to produce only the "odd" numbers, starting with 3 and stopping *before* 15.
>>> for number in range(3, 15, 2):
... if number % 2 == 0:
... print(f"{number} is even.")
... else:
... print(f"{number} is odd.")
...
'3 is odd.'
'5 is odd.'
'7 is odd.'
'9 is odd.'
'11 is odd.'
'13 is odd.'
अगर वैल्यू और उनके इंडेक्स दोनों चाहिए, तो बिल्ट-इन enumerate(<iterable>) (index, value) जोड़े लौटाता है:
>>> word_list = ["bird", "chicken", "barrel", "apple"]
# *index* and *word* are the loop variables.
# Loop variables can be any valid python name.
>>> for index, word in enumerate(word_list):
... if word.startswith("b"):
... print(f"{word.title()} (at index {index}) starts with a B.")
... else:
... print(f"{word.title()} (at index {index}) doesn't start with a B.")
...
'Bird (at index 0) starts with a B.'
'Chicken (at index 1) doesn\'t start with a B.'
'Barrel (at index 2) starts with a B.'
'Apple (at index 3) doesn\'t start with a B.'
# The same method can be used as a "lookup" for pairing items between two lists.
# Of course, if the lengths or indexes don't line up, this doesn't work.
>>> word_list = ["cat", "chicken", "barrel", "apple", "spinach"]
>>> category_list = ["mammal", "bird", "thing", "fruit", "vegetable"]
>>> for index, word in enumerate(word_list):
... print(f"{word.title()} is in category: {category_list[index]}.")
...
'Cat is in category: mammal.'
'Chicken is in category: bird.'
'Barrel is in category: thing.'
'Apple is in category: fruit.'
'Spinach is in category: vegetable.'
continue कीवर्ड की मदद से सीधे अगले इटरेशन पर पहुँचा जा सकता है:
word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple", "bear"]
# This will skip *bird*, at index 0
for index, word in enumerate(word_list):
if index == 0:
continue
if word.startswith("b"):
print(f"{word.title()} (at index {index}) starts with a b.")
'Barrel (at index 2) starts with a b.'
'Bongo (at index 3) starts with a b.'
'Bear (at index 6) starts with a b.'
break कीवर्ड (जैसा कि C से जुड़ी कई भाषाओं में होता है) इटरेशन रोक देता है और सबसे अंदर वाले loop से बाहर निकल आता है:
>>> word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple"]
>>> for index, word in enumerate(word_list):
... if word.startswith("b"):
... print(f"{word.title()} (at index {index}) starts with a B.")
... elif word == "sliver":
... break
... else:
... print(f"{word.title()} doesn't start with a B.")
... print("loop broken.")
...
'Bird (at index 0) starts with a B.'
'Chicken doesn\'t start with a B.'
'Barrel (at index 2) starts with a B.'
'Bongo (at index 3) starts with a B.'
'loop broken.'
आप एक शिक्षक के सहायक हैं और छात्रों की परीक्षा-कॉपियाँ जाँचते हैं। नतीजों का हिसाब हाथ से रखना अब उबाऊ भी लगता है और गलतियों से भरा भी। इसलिए आपने तय किया है कि कक्षा के नतीजे गिनने और निकालने के लिए कुछ फंक्शन बनाकर काम को थोड़ा दिलचस्प बना दिया जाए।
परीक्षा के सवालों पर आप "आंशिक अंक" दे सकते हैं, लेकिन पूरी परीक्षा का स्कोर int ही होना चाहिए।
इसलिए कक्षा के स्कोर के साथ कुछ और करने से पहले आपको सारे ग्रेड देखने होंगे और हर float स्कोर को int में बदलना होगा।
अच्छी बात यह है कि Python में पहले से ही round() फंक्शन मौजूद है, जिसे आप इस्तेमाल कर सकते हैं।
round_scores(student_scores) फंक्शन बनाइए, जो student_scores नाम की एक list लेता है।
इस फंक्शन को इनपुट list को खर्च करना है और सभी स्कोर को int में बदलकर एक नया ऐरे return करना है।
नतीजे वाले ऐरे में स्कोर का क्रम मायने नहीं रखता।
>>> student_scores = [90.33, 40.5, 55.44, 70.05, 30.55, 25.45, 80.45, 95.3, 38.7, 40.3]
>>> round_scores(student_scores)
...
[40, 39, 95, 80, 25, 31, 70, 55, 40, 90]
परीक्षा जाँचते समय आपने देखा कि कुछ छात्र उतना अच्छा नहीं कर पाए जितना आपने सोचा था। लेकिन आपका ध्यान भटक गया और आप यह लिखना भूल गए कि ऐसे कितने छात्र थे।
count_failed_students(student_scores) फंक्शन बनाइए, जो student_scores नाम की एक list लेता है।
इस फंक्शन को उन छात्रों की गिनती करनी है जिनके स्कोर पास होने लायक नहीं हैं, और वह गिनती पूर्णांक के रूप में लौटानी है।
परीक्षा में पास होने के लिए छात्र को 40 से अधिक स्कोर चाहिए।
>>> count_failed_students(student_scores=[90,40,55,70,30,25,80,95,38,40])
5
जिस शिक्षक की आप मदद कर रहे हैं, वे इस परीक्षा में "सबसे अच्छा" करने वाले छात्रों का समूह ढूँढना चाहते हैं। "सबसे अच्छा" किसे माना जाए, यह बदलता रहता है। इसलिए आपको वे स्कोर ढूँढने हैं जो मौजूदा सीमा से अधिक या उसके बराबर हैं।
above_threshold(student_scores, threshold) फंक्शन बनाइए, जो पैरामीटर के रूप में student_scores (ग्रेड की एक list) और threshold ("सर्वोच्च स्कोर" की सीमा) लेता है।
इस फंक्शन को वे सारे स्कोर एक list में लौटाने हैं जो threshold से >= हैं।
>>> above_threshold(student_scores=[90,40,55,70,30,68,70,75,83,96], threshold=75)
[90,75,83,96]
जिस शिक्षक की आप मदद कर रहे हैं, उन्हें संख्यात्मक स्कोर के साथ-साथ अक्षर ग्रेड देने भी पसंद हैं। परीक्षा में छात्र शायद ही कभी 100 स्कोर करते हैं, इसलिए "अक्षर ग्रेड" की निचली सीमाएँ सबसे ऊँचे स्कोर के आधार पर निकाली जाती हैं। ये सीमाएँ सबसे ऊँचे स्कोर और <= 40 की फेल होने वाली सीमा के बीच बराबर-बराबर बढ़ती हैं।
letter_grades(highest) फंक्शन बनाइए, जो आर्गुमेंट के रूप में परीक्षा का "सबसे ऊँचा" स्कोर लेता है और हर "अमेरिकी शैली" के ग्रेड अंतराल के लिए निचली स्कोर सीमाओं की एक list लौटाता है: ["D", "C", "B", "A"]।
"""Where the highest score is 100, and failing is <= 40.
"F" <= 40
41 <= "D" <= 55
56 <= "C" <= 70
71 <= "B" <= 85
86 <= "A" <= 100
"""
>>> letter_grades(highest=100)
[41, 56, 71, 86]
"""Where the highest score is 88, and failing is <= 40.
"F" <= 40
41 <= "D" <= 52
53 <= "C" <= 64
65 <= "B" <= 76
77 <= "A" <= 88
"""
>>> letter_grades(highest=88)
[41, 53, 65, 77]
आपके पास परीक्षा के स्कोर की एक ऐरे है, जिसमें स्कोर घटते क्रम में लगे हैं। इसी तरह छात्रों के नामों की एक और ऐरे है, जिसमें नाम भी उनके परीक्षा स्कोर के हिसाब से घटते क्रम में लगे हैं। आप हर छात्र के नाम को उसके परीक्षा स्कोर के साथ जोड़कर पूरी कक्षा की रैंकिंग छापना चाहते हैं।
student_ranking(student_scores, student_names) फंक्शन बनाइए, जिसके पैरामीटर student_scores और student_names हों।
student_names list में लिखे हर छात्र के नाम को student_scores list में उसके स्कोर से मिलाइए।
आप मान सकते हैं कि हर आर्गुमेंट list सबसे ऊँचे स्कोर वाले से सबसे कम स्कोर वाले क्रम में लगी होगी।
इस फंक्शन को ऐसी स्ट्रिंग्स की एक list लौटानी चाहिए जिनका प्रारूप <rank>. <student name>: <student score> हो।
>>> student_scores = [100, 99, 90, 84, 66, 53, 47]
>>> student_names = ['Joci', 'Sara','Kora','Jan','John','Bern', 'Fred']
>>> student_ranking(student_scores, student_names)
...
['1. Joci: 100', '2. Sara: 99', '3. Kora: 90', '4. Jan: 84', '5. John: 66', '6. Bern: 53', '7. Fred: 47']
परीक्षा में 100 का "परफेक्ट" स्कोर शायद ही मिलता है, लेकिन यह जानना दिलचस्प होता है कि क्या कम से कम एक छात्र ने वह हासिल किया है।
perfect_score(student_info) फंक्शन बनाइए, जिसका पैरामीटर student_info हो।
student_info एक list है, जिसमें हर छात्र का नाम और स्कोर रखने वाली कई ऐरे हैं: [["Charles", 90], ["Tony", 80]]।
इस फंक्शन को उस छात्र की सबसे पहली [<name>, <score>] जोड़ी return करनी चाहिए जिसने परीक्षा में 100 स्कोर किया हो।
अगर student_info में 100 का कोई स्कोर नहीं मिलता, तो खाली ऐरे [] लौटाया जाना चाहिए।
>>> perfect_score(student_info=[["Charles", 90], ["Tony", 80], ["Alex", 100]])
["Alex", 100]
>>> perfect_score(student_info=[["Charles", 90], ["Tony", 80]])
[]
Exercism पर साइन अप कीजिए और Python को 17 कॉन्सेप्ट146 अभ्यास तथा असली इंसानों से मिलने वाली मेंटरिंग के साथ सीखिए और उसमें महारत हासिल कीजिए, वह भी बिल्कुल मुफ्त।