for loop is used to retrieve all data items from any sequence. Here the sequence
may be any collection or any string value
fruits=['apple','banana','cherry','orange']
for k in fruits:
print(k)
apple
banana
cherry
orange
for x in 'cherry':
print(x)
Output:
c
h
e
r
r
y
#break : It is a transfer statement.
#When a break statement executes based on a condition inside a loop, then the loop will terminate.
fruits=['apple','banana','cherry','orange']
for k in fruits:
print(k)
if k=='cherry':
break
apple
banana
cherry
fruits=['apple','banana','cherry','orange']
for k in fruits:
if k=='cherry':
break
print(k)
apple
banana
#continue : It is another transfer statement
#When a continue statement executes based on a condition inside a loop, an iteartion will skip.
fruits=['apple','banana','cherry','orange']
for k in fruits:
if k=='cherry':
continue
print(k)
apple
banana
orange
#range(): It generates a sequence of numbers.
# The first no is 0 by default, increments by 1 by default and end at a specified no.
# It may take one argument or two arguments or three arguments.
for x in range(6):
print(x)
0
1
2
3
4
5
#range() taking two arguments
for x in range(2,6):
print(x)
3
4
5
#range() taking three arguments
for x in range(2,30,3):
print(x)
2
5
8
11
14
17
20
23
26
29
#Nested Loop [When a loop is embeded in another loop is known as nested loop]
#Here one is considering as outer loop and one is considering as inner loop
list1=['red','big','tasty']
list2=['apple','banana','cherry']
for x in list1:
for y in list2:
print(x,y)
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
#pass statement
for x in [10,20,30]:
pass
print('welcome')
-> welcome
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc