Now consider writing that code 100 times. That would be quite painful wouldn't it? Now consider writing that code 10,000 times. It isn't just bad, it is a nightmare. Luckily for us, Python has a way of solving this problem for code and that way is through loops. In this article, I will cover what loops are, when you should use loops and why you will soon have loops in your toolbox.
Two official python loop statements are for and while loops.
-
forloops: They are ideal for going through a sequence (like a list, tuple, or string), one by one. We useforloops when we know how many things there are in a list.For example, for each element
iinlist_of_numbers, print out the value ofi:
Syntax and structure
The syntax of a for loop in Python is straightforward. It includes the for keyword, an item variable, the in operator, and a collection object, where collection refers to an object we can iterate through, like a list, dictionary, or tuple. The instructions we want to run in each loop are in the loop body.
For example:
Here the loop accesses each value in the list, one by one. We can iterate through our list of animals too, one by one:
We can add as many instructions as we like in the body of the loop. Once a loop is complete, it executes the next line of code after the loop.
We can loop through any iterable data structure like tuples, sets, dictionaries, and even strings:
While loops
A while loop in Python repeatedly executes its body as long as a condition is true. Its syntax is:
while loops are ideal for scenarios where you don't know how many times you need to iterate. For example, adding 10% to a value, until it is > 5. It is hard to know before we start how many times we have to repeat the calculation, so if we use a while loop, we can keep going until we get to the answer.It can be stopped if the condition is false or by using break key word.

This loop prints the population with 10% added to each iteration, until the population is 2.85, and the loop stops because the next number is larger than 3.
Be cautious with while loops, because they result in infinite loops if the condition never becomes false. Always ensure there's a way for the condition to become false eventually.
If your loop is infinitely looping, press the i key twice rapidly, or click on the stop/interrupt button to interrupt the kernel. The cell below contains an example of an infinite loop. It will go into an infinite loop when running it, so beware!
Loop controls
Loop control statements are used to change the execution flow of loops. They provide flexibility and control over how loops behave in different scenarios. The primary loop control statements in Python are break, continue, and pass.
break statement
break immediately stops the execution of a loop, and the control flows to the statement immediately after the loop.
break is often used when searching for an item in a list. Once the item is found, there's no need to continue the loop.
The loop stopped once we reached the African Elephant animal, so printing out print(f"The last animal checked = {animal}") after the loop has completed, results in African Elephant, showing that the loop stopped there.
continue statement
continue skips the rest of the loop's body for the current iteration and moves to the next iteration.
This is useful when we need to skip certain elements in a loop, like filtering out specific values.
pass statement
pass is a statement that does nothing. It's used as a placeholder.
pass is used when we need a statement to have the correct syntax but want to implement no action, like in empty definitions, loops, or conditionals. pass is very useful as a placeholder for the code we are going to add later.
Suppose we want to check all animals, but skip African Elephants:
We've learned that loops are essential for automating repetitive tasks and efficiently processing collections of data. By understanding the nuances of for and while loops, as well as loop control statements like break, continue, and pass, we can write more effective and efficient Python code.













Top comments (0)