Loops
Python Basics • Last Updated: 03/12/2025
Python Basics • Last Updated: 03/12/2025
Ever gotten tired of typing the same code over and over again multiple times in a sequence? Well no problem! You can easily automate this process with what's called 'for loops'. What they do is that they repeat a certain chunk of code over and over again for a specified amount of times. You can also create infinite loops too, all covered in this topic.
Loops in Python are useful for repeating certain segments of code for a specified amount of times, or indefinitely. This can be done using for i in range(10):.
The code mentioned above basically loops anything below it ten times. If you were to print i to the output box in each iteration, you would get 0 to 9, printed in order. Python when counting always starts at zero. Unless you want it to print i and have it start from 1, you can just do this:
print(i+1)
NOTE: Any code referring to i will only work inside the loop. Otherwise, you'll have to have had already defined the value for i. Also, i can be any letter, but i is usually preferred.
You can create basic loops for a specified amount of time using code like this:
The script above basically counts from 0 and ends at 9, as that is a total of 10 values. Then it prints the value of i to the output box, which looks like this:
If you are printing values of i and wanting to print only values at a specific interval, then you can simply just use the same code with some adjustments. You need three numbers instead of the original loop count, separated by commas. Those three numbers, in respective order are the starting number, the ending number (excluded in result), and the number you count by (if it's 2, then it'll be 2, 4, 6, 8, so on...). Here's an example of this using variables to hold the number data:
The output will look like this:
You can change these values, (2, 10, 2), to any number(s), and will pretty much do similar things. Just make sure the middle value is the largest of the lot, because you won't be starting at a number, and skip count to a number by a larger number (e.g. counting to 10, skip-counting by 15, which makes no sense).
You also don't need to have the variables, just, the actual numbers in there works just fine.
Tables are a simple concept. You can store multiple values in variables, which are called 'tables', or 'lists'. You can print each item in a list in order using for var in list. var is the iterative variable, and list stores the relevant data as list-form to be used by the loop. Here's an example of how you can use lists in your for-loop.
As a simple overview of tables, tables are defined by square brackets as the opening and closing tags. You separate each item using commas, and strings are placed in quotations.
The output of the script above would look like this:
And that's because we are printing each item from the list, list, to the output box in order.
While you can create loops that iterate over a specified amount of times, you can also build loops that loop, forever. The only issue is that to get it to stop running is to break out of it using the keyword, break on its own line within the loop.
Past the explanation, how do you make infinite loops? It's simple. You just have to add this:
Now that you've created your infinite loop, you need a way to close it. You can do this by using conditional breaks, which means, having an if/else statement that checks for something, then breaks out of the loop if the conditions are met. How do you do this? Well here's an example.
This code sets a value for the variable, x, as 0. Next, it runs a loop for each time x is less than 5. It prints x once during each iteration and increases the value of x by 1, hence the += operation that is used inside the loop.
Essentially, it's pretty much just a loop that checks if x is greater than 5 and deciding if it should run the internal code based on that and if it should move on.
Breaks are how you exit out of a loop if for example, a condition for an if/else statement is met. You can use breaks to stop infinite while loops from running forever and causing RAM and data issues on your device. An easy fix is the break command. It's easy to implement, as you only need to have it on its own line in a loop or statement and it closes the loop early.
Here's how you can use it:
... essentially, now, this loop still counts from 0 to 4, (and prints the results), but this time, we're checking if the variable, x, has a value greater than 2, then breaking out of the loop if that's the case, which stops the code.
Similar to 'break', the continue command works exactly the same. The only difference is instead of breaking out of the loop, it just lets the loop know that it has to skip the current iteration of code in the loop and move on to the next. You can use it just like this:
The way this code works is now, instead of breaking out of the loop when x is greater than 3 (e.g. 4 in this scenario), we check if x is equal to 3. And if it is, then we move on to the next iteration in the loop, ending with '3' not being printed at all):
There will come an instance where your code breaks due to lag. A major factor is infinite loops. In cases like this, you'll need to check for any while loops in your code. You can easily do this by hitting Ctrl+F on Windows or Cmd+F on Mac. That will most likely open up a search menu where you can search 'while', which can bring up potential instances of any while loops.
Then check, do you ever close those loops? Because in order to close them, the loop will need an end, either by maximum iterations from embedded 'if' statements, (e.g. while var < 5), or using breaks. If none are there, then adding either one or both is the way to fix the issue.
Loops are a real timesaver when you wish to not type up the same code over, and over, and over again. That can get boring. A fix is loops. Loops are easy to implement and work for any specified amount of iterations or based on variable data. You can also use them with lists. Infinite loops are useful, but can cause lag if un-monitored. That's where breaks and continues come in, as they can be placed straight up or from if-statements to end the loop or iteration early and move on to the next segment of code (or iteration within the loop).
Feel free to ask questions or request updates in the comments.
Share your opinions or ask questions.
-