Home
Aptitude
Categories


Home » Python » Python programs

Python | Examples of loops (based on their control)



Python loop examples based on their control: Here, we are writing examples of Range Controlled loop, Collection Controlled, Condition Controlled Loop.
Submitted by Pankaj Singh, on October 09, 2018

Based on loop controls, here are examples of following types:

  1. Condition Controlled Loop
  2. Range Controlled loop
  3. Collection 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

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.