
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
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)
अगर हम loop में 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 करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
1 Comment
nice explanation