Python loops examples dependent on their control: Here, we are composing examples of Range Controlled loops, Collection Controlled, Condition Controlled Loop.
In view of loops controls, here are examples of the following kinds:
- Condition Controlled Loop
- Range Controlled loop
- Assortment Controlled Loop
1) Condition Controlled Loop
# Condition Controlled Loop
a=1
while a<=10:
print(a)
a=a+1
Output:
1
2
3
4
5
6
7
8
9
10
Range Controlled loop
#Range Controlled Loop
#range(end) start=0,step=+1
for i in range(10):
print(i,end=" ")
print("\n")
#range(start,end) step=+1
for i in range(1,11):
print(i,end=" ")
print("\n")
#range(start,end,step)
for i in range(1,11,3):
print(i,end=" ")
print()
for i in range(10,0,-1):
print(i,end=" ")
Output:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 4 7 10
10 9 8 7 6 5 4 3 2 1
Assortment Controlled Loop:
#Collection Controlled Loop
fruits=["apple","banana","guava","grapes","oranges"]
for item in fruits:
print(item)
Output:
apple
banana
guava
grapes
oranges