Home >> Python Tutorial >> Python Loops
Edit Template
Edit Template

Loops in Python in hindi

What is Loops in Python

python मे अगर हमे किसी block of code को बार बार चलाना होता है तो हम loops का use करते है। python मे 2 types के loop होते है-

1. For Loop 

2. While Loop 

What is While Loop

Python में जब हमें किसी block को Repeatedly execute करना होता है तो हम while loop का use करते हैं  while loop एक control flow statement है यह किसी block को तब तक execute करता है जब तक condition true होती है

How While Loop Work

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 से समझते हैं

🐍
while.py
Copy to clipboard
num = 1  # Initialization
while num <= 5:  # Condition
    print(num)
    num += 1  # Increment

Nested While Loop

जब हम while loop के अंदर while loop को used करते हैं तो इसे nested while loop बोला जाता है चलिए इस एक example के through समझते हैं

🐍
nested while.py
Copy to clipboard
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

What is For Loop

for loop एक control flow statement है इसका use हम generally sequence (जैसे –  list, set, string, etc.) के Elements पर perform करने के लिए करते हैंFor loop elements को sequence से एक-एक करके retrieve करता है 

Example के through समझते हैं

🐍
for.py
Copy to clipboard
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

What is Range Function

range एक function है जिसका  use numeric sequence generate करने के लिए होता है यह function एक  sequence के number को  return करता है जो का  look में  I treat किया जा सकता है

How Range Function Work

 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 समझते हैं

🐍
range.py
Copy to clipboard
for num in range(1, 10, 2):  # Step = 2
    print(num)

Nested For Loop

अगर हम For loop के अंदर भी for loop का use करते हैं तो इसे nested for loop बोला जाता है

Example से समझते हैं

🐍
nested for.py
Copy to clipboard
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

What is Break Statement

अगर हमें Loop को Permanently terminate करना होता है तो हम Break का use करते हैं, लिए break को अच्छे से समझने के लिए एक example देखते हैं

🐍
break.py
Copy to clipboard
for num in range(1, 6):
    if num == 3:
        break  # Stops when num = 3
    print(num)

What is Continue Statement

अगर हम loop में current Iteration को  skip करके next iteration पर jump करना चाहते हैं तो continue statement का use करते हैं

 example के  through अच्छे से समझते हैं

🐍
continue.py
Copy to clipboard
for num in range(1, 6):
    if num == 3:
        continue  # Skips 3
    print(num)

उम्मीद करते है कि आपको loops in python अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

1 Comment

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top