In last part we have discussed about statement and its syntax and how we can use in our program. Now we will discuss about loops in python.
In programming there can be a scenario when you have to execute the block of code multiple time. This can’t be done by normal sequential programming. For this reason, all programming languages provide the looping structure to run a block of code multiple time. There are multiple types of looping that is offered in python. The name of the loops is following:
- For Loop
- While Loop
- Nested for Loop
We will see all these loops and the variation between them. First, we will see for loop.
For loop is a type of looping that starts with a keyword for and take the range of number from where to start and where to end the loop. Consider the following example:
As the output show the loop start from 0 and end before 10. The range keyword takes two arguments the starting point of loop and ending point of loop.
Range with 1 argument:
When we use range with only one argument it assigns the starting point of loop be default zero. The above code can be written with only one range argument:
Range with 3 argument:
If we want to set the increment after each iteration than we have to give third argument of range. By default, the increment argument is assigned as one. Following is the code incremented by 2:
Similarly, we can do negative increment by passing negative value as 3rd argument.
For Loop without Range:
We can use for loop without using range function. This for looping is also known as element extraction. We can do for loop on strings, list, dictionaries and everything that can be iterated. Following is the example of applying for loop on a string:
Check if number is prime or not:
Print all the numbers that are multiple of 5:
The above example start loop from 0 and ends on 100. It will print all the number that are completely divisible by the digit 5 which will result in the printing of all the digits that are multiple of 5.
Nested for Loops:
Nested for loop is another type of for loop. It is basically for loop with in a for loop. For every iteration of outer for loop it will execute the complete inner for loop. Its implementation can be understood by the following code:
While loop is like the for loop. The functionality of both the loops are same but the syntax is totally different. We will write the same Check prime code that we have written in for loop with a while loop to know the difference between the syntax of for loop and while loop:
The above code is the same that we have written in for loop. The only difference is we are using a while loop and at every end of the loop we are incrementing the variable plus 1.
The above loop will iterate for infinite time. It will not stop until the program is closed by the user. Such type of looping is used when the servers are running or some other type of computation like getting the frames from the camera input. For running such type of loop, we must use many types of error exception so that it cannot be stopped from some bad argument and continue looping. These sorts of error exception will be studied in later modules. So, stay tune to learn more exciting aspect of python programming.