Loops in Python

                            Loops

While programming sometimes there are conditions where we need to execute a block of code to execute several times and loops makes this task easy for us. Every programming language supports loops. Generally there are three types of loops i.e. for, while and do-while. But in Python we have only two types of loops while and for. In loops we have a condition and block of code. We execute the code till the condition satisfies. Loops can be used in netsed forms also.

While loop : 
In while loop we use the while keyword and code block runs till the condition satisfies.
syntax - while condition:
               statements
E.g. i = 0
    while i < 10:
        print(i, end = " ")
        i += 1

       Output : 0 1 2 3 4 5 6 7 8 9

In above example the code runs if i is less than 10 and inside block we increment the value of i. When the value of i becomes greater than or equal to 10 the condition breaks and we comes out of loop. Now we have used the "end" in our example. In python when we use the print function it breaks the lines of output screen by default, means the next printable line will be on next line. But end = " " makes the space and append the next output result with the current result with space. If instead of space we use the *, # or anything else then that will seprate the output results.

For loop : 
In for loop we use the for keyword. Basically in for loops we iterate over some data like string, array, list, tuple, etc. We traverse the data using for loop.
syntax - for iterator in data:
              statements

E.g. i = 0
    for i in range(10):
        print(i, end = " ")

        Output : 0 1 2 3 4 5 6 7 8 9

In above example i iterates over the range of 10 when this range finishes the loop breaks. range() function is used to defined the range of given value. Basically it takes 3 parameters - starting value, ending value and gap. By default the starting value of 0 and gap is 1. So in the example range starts from 0 to 10 with the gap of 1. Now the point to remember is that in range function starting value is inclusive but ending is not. That's why loop starts from 0 and ends at 9.

We can also iterate over the data.
E.g. name = "Thor"
   for i in name:
        print(i, end = " ")
    
        Output : T h o r

We can do the same with lists, tuples, sets, dictionary and other iterable data types.

Continue : In loops continue keyword is used to skip the executable code and jumps to the starting of loop. Let's see the example.
E.g. for i in [10, 20, 30, 40, 50]:
            if i == 30:
                continue
            else:
                print(i, end = " ")
        
        Output : 10  20  40  50

You can see that 30 does not print because we skip that using continue. Basically continue represents that we do not just continue to the loop without executing any more statements.

Break : In loop we can use break keyword to break the loop, means we comes out of loop. It is different than continue. In break our loop gets over after seeing the break statement.
E.g. for i in [10, 20, 30, 40, 50]:
           if i == 30:
                break
           else:
                print(i, end = " ")

        Output : 10  20

Infinite loops : This is the problem of loops if we are not careful while making the conditons of loop. If we have the condition in loop that never gets over of never gets false then our loop will run infinte number of times. 
E.g. while 1:
        print("Hello", end = " ")

        Output :  Hello  Hello  Hello  Hello ..................
This loop will run forever.

Enumerate : In Python enumerate() function adds the counter to the iterable data and returns that in form of tuple. In enumerate() we give the iterable data and starting point. By default starting point is 0.
E.g.  l = [10, 20, 30]
    for x in enumerate(l):
        print(x)
    
       Output :  (0, 10)
                           (1, 20)
                           (2, 30)

Zip : In python zip() function is used to zip the multiple iterable data together and forms the tuple for each value of same index from all the iterables. In zip() we can have multiple number of iterables. But remember that data will zipped together for the minumum numbers of values possible. Suppose that in first iterable we have 5 items, in second one we have 7 items and in third one we have 10 items. Then we can use the zip function but we have only 5 tuples formed because thats the least number of items among all iterable data.
E.g. l1 = [1, 2, 3, 4]
    l2 = [10, 20, 30, 40]
    l3 = [100, 200]
    for i in zip(l1, l2, l3):
         print(i)
    
    Output : (1, 10, 100)
                       (2, 20, 200)




Comments

Popular posts from this blog

Python OOPs

Regular Expressions