Q:

Python | Examples of loops (based on their control)

belongs to collection: Python basic programs

0

Python | Examples of loops (based on their control)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

2) 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

3) Collection Controlled loop

#Collection Controlled Loop
fruits=["apple","banana","guava","grapes","oranges"]
for item in fruits:
    print(item)

Output

apple
banana
guava
grapes
oranges

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Some of the Examples of loops... >>
<< Python | Demonstrate an example of for each loop...