
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
python मे अगर हमे किसी block of code को बार बार चलाना होता है तो हम loops का use करते है। python मे 2 types के loop होते है-
1. For Loop
2. While Loop
Python में जब हमें किसी block को Repeatedly execute करना होता है तो हम while loop का use करते हैं while loop एक control flow statement है यह किसी block को तब तक execute करता है जब तक condition true होती है
while loop start होने से पहले Condition को Check करता है अगर Condition true होती है तो loop के अंदर का Code execute होता है एक बार execution होने के बाद block के अंदर condition update होता है और Condition true होता है तो code का execution फिर से होता है और यह प्रक्रिया तब तक चलता रहता है जब तक Condition False ना हो जाए. इसलिए हमें Condition को सावधानीपूर्वक देना होता है वरना loop infinite हो जाएगा और code का Execution कभी रुकेगा ही नहीं
example से समझते हैं
num = 1 # Initialization
while num <= 5: # Condition
print(num)
num += 1 # Increment
जब हम while loop के अंदर while loop को used करते हैं तो इसे nested while loop बोला जाता है चलिए इस एक example के through समझते हैं
i = 1
while i <= 3: # Outer loop (Rows)
j = 1
while j <= 3: # Inner loop (Columns)
print("*", end=" ") # Print star in the same row
j += 1
print() # Move to the next line
i += 1
for loop एक control flow statement है इसका use हम generally sequence (जैसे – list, set, string, etc.) के Elements पर perform करने के लिए करते हैंFor loop elements को sequence से एक-एक करके retrieve करता है
Example के through समझते हैं
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
range एक function है जिसका use numeric sequence generate करने के लिए होता है यह function एक sequence के number को return करता है जो का look में I treat किया जा सकता है
Start : यह Range की starting value होती है जो by default 0 होता है मतलब अगर हमने range में starting value नहीं दिया है तो for loop by default इस value को 0 मानेगा but अगर हमने कोई value दिया है तो और loop वहीं से शुरू होगा
Stop : यह ending value होती है हम range में stop की value जितनी रखेंगे और look उतना बार code को Execute करेगा
Step : यह increment value होती है जो red fault 1 रहता है मतलब हम range में increment value ना देतो इसका By default 1 एक माना जाएगा मतलबHot का execution एक-एक step में करेगा
example के through समझते हैं
for num in range(1, 10, 2): # Step = 2
print(num)
अगर हम For loop के अंदर भी for loop का use करते हैं तो इसे nested for loop बोला जाता है
Example से समझते हैं
for i in range(3): # Outer loop (rows)
for j in range(3): # Inner loop (columns)
print("*", end=" ") # Print in same line
print() # Move to next line
अगर हमें Loop को Permanently terminate करना होता है तो हम Break का use करते हैं, लिए break को अच्छे से समझने के लिए एक example देखते हैं
for num in range(1, 6):
if num == 3:
break # Stops when num = 3
print(num)
अगर हम look में current Iteration को skip करके next iteration पर jump करना चाहते हैं तो continue statement का use करते हैं
example के through अच्छे से समझते हैं
for num in range(1, 6):
if num == 3:
continue # Skips 3
print(num)
उम्मीद करते है कि आपको loops in python अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...
Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...
Scikit-learn Tutorial Scikit-learn Hamburger Toggle Menu Edit Template Scikit learn in Hindi What is Scikit-learn in Hindi Scikit learn एक...
1 Comment
nice explanation