Edit Template
Edit Template
Edit Template

Loops in Python

What is Loops in Python

Loops in Python are used to execute a block of code multiple times. Python provides 2 types of loops: For Loop and 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 से समझते हैं

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

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

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

for num in range(1, 10, 2):  # Step = 2
    print(num)

Nested For Loop

अगर हम 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

What is Break Statement

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

for num in range(1, 6):
    if num == 3:
        break  # Stops when num = 3
    print(num)

What is Continue Statement

अगर हम 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 करे,

Some important questions in this tutorial

What is loops in python, Python me loops kya hota hai.

1 Comment

  • vishal

    nice explanation

Add a comment...

Your email address will not be published. Required fields are marked *

  • 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
Python List

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python String

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python Loops

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Conditional Statements

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Python Operators

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Python Data Type

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Scikit-learn

Scikit-learn Tutorial Scikit-learn Hamburger Toggle Menu Edit Template Scikit learn What is Scikit-learn Scikit learn एक python library है जिसका...

Pandas

Pandas Tutorial Pandas Hamburger Toggle Menu Edit Template Pandas What is Pandas pandas  एक python library है, जिसका use हम...

NumPy

NumPy Tutorial NumPy Hamburger Toggle Menu Edit Template NumPy What is NumPy matplotlib का use हम data visualization के लिए...

Edit Template
Scroll to Top