Menu

Different ways of writting loops in Python

Lets see we have a list of inetgers name x. 

x = [3,3,3,4,4,4,3,3,5,5,6,6,7,8,9,9,9]

Now iterate over this list using differnet loops.


  1. For loop

          #To read all  the elements from list

for elem in x:
    print(elem)


       #To read element from specific index using range(startindex, stopindex)

        for i in range(2, len(x)):

            print(x[i])

 

  1. While loop
    i = 4
while i < len(x):
    print(x[i])
    i+=1

    No comments:

    Post a Comment