The Art of Iteration — Python Loops

Timothy Rudenko
3 min readNov 4, 2022

--

Loops are an essential part of any programming language. They allow us to repeat a process multiple times, and they allow us to iterate through ordered data.

Photo by 愚木混株 cdd20 on Unsplash

For Loops

Loops have many advantages; the most basic and obvious is that we can repeat a process as much as we want. Why would we want to run a piece of code multiple times? A good example would be a situation where we need to perform a calculation for every number in a range.

Let’s pretend we want to find the result of every number cubed between 1 and 100.

We start by writing the word “for” to tell Python we’re using a for loop. We then create a variable on the same line (number) that represents each number in our range from 1 to … 101? Is that a mistake? No, it’s one of the quirks of the range function. We specify where we start (1) and stop (101). The reason we put 101 instead of 100 is that the range function will go up to but will not include the number we specify. So if we use 100, our loop will go only to 99.

This loop will print out the cubed value of all the numbers to 100. Having to do this manually would not only be hard to maintain, but it would also be a massive waste of time.

For loops aren’t just for numbers and ranges, they can be used through anything that’s iterable. Without going down the rabbit hole, an iterable is something that has a sequence, like a string.

Again in English, this is us saying: “For every letter in ‘Timothy’; output the letter.”

That is the basics of a for loop in Python, it’s versatile and there are too many use cases to count.

While Loops

What if we don’t know how many times to repeat a process? What if we want to run code only if it needs to? A for loop would be hard to use in that case. That is where the while loop comes into play.

A while loop requires a conditional statement to evaluate. If you have trouble with conditional statements, I recommend you read the previous article.

The loop will execute every time the conditional evaluates to True in Python.

In the above example, our while loop checks to see if num is less than or equal to 11. If it is, it will first print num, then it will add 1 until it finally reaches 11.

While loops are also great for input validation

We use a while True here to make an infinite loop. If the condition in our while loop is True, the code runs.

We ask the user to enter an integer; If the input is not in the range of 0–100, we output a message to the user and repeat; otherwise, we break out of the loop and continue with our program.

Practice

In programming, success won’t come just by understanding conceptually. Without practice, you’ll forget; you will be right back where you started. I want this to be the only time you read this article.

Get after it

--

--

Timothy Rudenko

Former Red Hat SRE and Python instructor. I write about Programming, Linux, and Workflow. Learn technology in a foundational and reliable way.