For Loop In Python - TECH WORTHY MIND
We can execute "for loop" in python as long as the condition is true.
Python has two primitive loop commands:
• while loops
• for loops
for loop :
The syntax of for loop is simple and easy to use. We can access the set, tuple, list, dictionary, etc elements by using for loop.
Print list elements using for loop
list=["mango","banana","orange"]
for x in list:
print(x)
Output: mango
banana
orange
Similarly, we can access the element of the remaining data structure in the same way.
Nested loop in Python:
for x in range(4):
for y in range(2):
print(f'({x}, {y})
Output:
(0,0)
(0,1)
........
(4,2)
Q.1- write a program to find the largest number in a list using for loop.
Ans-
Numbers=[7,9,19,50,52]
max=Numbers[0]
for Number in Numbers:
if Number>max:
max=Number
print(max)
Output: 52
Comments
Post a Comment
DON'T COMMENT LINK.