Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
लूप्स
Python में दो प्रकार के लूप होते हैं, for और while।
"for" लूप
For लूप दिए गए अनुक्रम के ऊपर आवृत्ति करते हैं। यहाँ एक उदाहरण है:
For लूप "range" और "xrange" फ़ंक्शनों का उपयोग करके संख्या के अनुक्रम के ऊपर आवृत्ति कर सकते हैं। range और xrange के बीच का अंतर यह है कि range फ़ंक्शन निर्दिष्ट अनुक्रम के साथ एक नई सूची लौटाता है, जबकि xrange एक iterator लौटाता है, जो अधिक कुशल होता है। (Python 3 में range फ़ंक्शन का उपयोग किया जाता है, जो xrange की तरह कार्य करता है)। ध्यान दें कि range फ़ंक्शन शून्य आधारित है।
"while" लूप
जब तक एक निश्चित boolean स्थिति है, While लूप दोहराते हैं। उदाहरण के लिए:
"break" और "continue" कथन
break का उपयोग for लूप या while लूप को छोड़ने के लिए किया जाता है, जबकि continue का उपयोग वर्तमान ब्लॉक को छोड़ने के लिए किया जाता है, और "for" या "while" कथन पर लौटता है। कुछ उदाहरण:
क्या हम लूप के लिए "else" खंड का उपयोग कर सकते हैं?
C, CPP जैसी भाषाओं के विपरीत, हम लूप के लिए else का उपयोग कर सकते हैं। जब "for" या "while" कथन की लूप स्थिति विफल हो जाती है, तो "else" में कोड भाग निष्पादित होता है। यदि एक break कथन for लूप के अंदर निष्पादित होता है तो "else" भाग छोड़ दिया जाता है। ध्यान दें कि "else" भाग तब भी निष्पादित होता है जब continue कथन होता है।
यहां कुछ उदाहरण दिए गए हैं:
Exercise
संख्याओं की सूची के सभी सम संख्याओं के माध्यम से लूप करें और उन्हें उसी क्रम में प्रिंट करें जिसमें वे प्राप्त हुए थे। अनुक्रम में 237 के बाद कोई संख्याएँ प्रिंट न करें।
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527
]
# your code goes here
for number in numbers:
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527
]
# your code goes here
for number in numbers:
if number == 237:
break
if number % 2 == 1:
continue
print(number)
test_object("number", undefined_msg="Define a object `number` using the code from the tutorial to print just the desired numbers from the exercise description.",incorrect_msg="Your `number` object is not correct, You should use an `if` statement and a `break` statement to accomplish your goal.")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!