Loops in Python
# Example 1: while loop
i=0
while i<5:
i=i+1
print(i)
OUTPUT
1
2
3
4
5
# Example 1: for loop
a="Hi Ravin"
for item in a:
print(item)
OUTPUT
H
i
R
a
v
i
n
# Example 2: for loop
a="My name is Ravin"
a1=a.split()
for item in a1:
print(item)
OUTPUT
My
name
is
Ravin
# Example 3: for loop
for i in range(5):
print(i)
OUTPUT
0
1
2
3
4
# Example 4: for loop
for i in range(1,5):
print(i)
OUTPUT
1
2
3
4
# Example 1: break
for i in range(1,5):
print(i)
if i==2:
break
OUTPUT
1
2
# Example 1: Continue
for i in range(1,5):
if i==2:
continue
print(i)
OUTPUT
1
3
4