Python Loops Notes for Beginners
What is a Loop?
Loop ka matlab hota hai kisi kaam ko baar baar repeat karna.
Python me loops use hote hain:
● Repeated tasks ke liye
● Time bachane ke liye
● Same code ko multiple times chalane ke liye
Types of Loops in Python
1. for loop
2. while loop
For Loop
For loop kisi sequence ko repeat karta hai.
Syntax
for variable in sequence:
print(variable)
Example 1
for i in range(5):
print(i)
Output:
, 0
1
2
3
4
Example 2
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
While Loop
While loop tab tak chalta hai jab tak condition true ho.
Syntax
while condition:
print("Hello")
Example
x=1
while x <= 5:
print(x)
x=x+1
Output:
What is a Loop?
Loop ka matlab hota hai kisi kaam ko baar baar repeat karna.
Python me loops use hote hain:
● Repeated tasks ke liye
● Time bachane ke liye
● Same code ko multiple times chalane ke liye
Types of Loops in Python
1. for loop
2. while loop
For Loop
For loop kisi sequence ko repeat karta hai.
Syntax
for variable in sequence:
print(variable)
Example 1
for i in range(5):
print(i)
Output:
, 0
1
2
3
4
Example 2
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
While Loop
While loop tab tak chalta hai jab tak condition true ho.
Syntax
while condition:
print("Hello")
Example
x=1
while x <= 5:
print(x)
x=x+1
Output: