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

Popular posts from this blog

How To Hack Wifi | Hack Wifi Password | TECH WORTH MIND

Remove Tools From Termux | How can I reopen the installed tool in termux? | TECH WORTHY MIND