There are two types of Loops in python, the while loop and the for loop. Each have their own unique uses and syntax, which we’ll briefly explain in the tutorial below.
Loops help us avoid senseless repetition in our code. Instead of writing 10 if else statements separately, we can use loops to write just one which loops over 10 times.
Unlike other languages like C++which use brackets, Python’s loops rely solely on indentation to define the code within them. If you haven’t properly indented your code, you might run into an error or your program will not function the way you wish. Pay close attention to our examples!
If you’re someone coming from another programming language like C++ or Java, you should know that Python does not have “Do While Loops”.
|while||Repeats a block a code while a given condition holds true.|
|for||Repeats a block of code for a given number of times. Can also be used to iterate through collection types variables, like Lists and Arrays.|
You can further increase the functionality of these loops by creating “Nested loops”. Nested loop refers to a loop that exists within another loop.
Python Loop control:
Loop control statements help us manipulate Python loops during their execution.
|break||When a loop encounters this statement during its execution, it will immediately stop and “break”, moving to the next statement after the loop.|
|continue||The loop will stop it’s current iteration, and move on to the next iteration.|
|pass||pass statement is required to be used in empty loops, due to the requirement that loops must have some code within them. Basically, you use it when you don’t want any code to be executing.|
Python Loops Examples:
Be vary of capitalization while writing loops, everything must be in lowercase.
While the condition x < 5 holds true, the loop will continue running. Once the condition x < 5 becomes false, the loop terminates.
x = 0 while x < 5: print(x) x = x + 1
0 1 2 3 4
This for loop iterates over the list “shopping” outputting each element separately.
shopping = ["Apples","Oranges","Grapes"] for x in shopping: print(x)
Apples Oranges Grapes
A Nested loop is a loop within another loop. Using Nested loops in our code can prove very useful and help us iterate through sequences in new ways. The most common use of Nested loops comes in iterating through other Nested or multi-dimensional items like 2-D arrays and Nested Lists.
What’s happening here is that there is a List, which further contains two lists within it. So its a “nested list”. If we iterate through it normally, we’ll just get 2 lists, which you can even see in the example output. Iterating through the list twice gives us each element separately.
shopping = [["Apples","Oranges","Grapes"],["Pots","Spoons","Forks"]] for x in shopping: print(x) for i in x: print(i)
['Apples', 'Oranges', 'Grapes'] Apples Oranges Grapes ['Pots', 'Spoons', 'Forks'] Pots Spoons Forks
While there is no limit to the amount loops you can nest into each other, for readability’s sake avoid going above 3. If you have 5 layers or more of nested loops or if statements, you’re likely doing something wrong.
This marks the end of the Python Loops article. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions can be asked in the comments section below.