Python Loops
Now, we are going to explore a very interesting concept, which is looping in Python. Basically, there can be a number of situations, where we would need to make use of the looping statements, and we are going to explore these looping statements.
Well, with looping, we simply mean that we need to do something repeatedly, for a number of times, or till some condition is satisfied, or simply we can say that we can iterate over a sequence, using the loop. You would understand why, and how we can make use of the loops, in Python, as we move ahead. We are going to learn about the for loops, and the while loops soon.
Python Loops
Let’s consider a situation, where we are required to print all the prime numbers from 1 to 100. In such a situation, we are doing one thing repeatedly, we are going to get a number between 1 to 100 sequentially, and then we are checking if that number is prime or not. If the number is prime, we are printing it. So, since we are doing this thing repeatedly, we do not need to write the same code for getting a number from the sequence, then checking if the number is prime, and then printing the number. We can just put the things in the loop, and then perform them repeatedly.
If you are not able to visualize the concept, do not worry, since we are going to look into the individual loops soon, and at that time, we would go into depth about the loops.
Here is an insight into what we are going to cover –
- for loop
- while loop
- break keyword
- continue keyword
- pass keyword
- using else with loops