Loops In Python
There are two kinds of loops in Python: for loops and while loops. The basic idea behind any kind of loop is to run a section of code repeatedly as long as a specific statement (called the test condition) is true. For example, try running this script that uses a while loop:
n = 1
while (n < 5):
print(“n =”, n)
n = n + 1
print(“Loop finished”)
Here we create a variable n and assign it a value of 1. Then we start the while loop,The statement that we are testing comes in parentheses after the while command; in this case we want to know if the statement n < 5 is true or false . Since 1 < 5 , the statement is true and we enter into the loop after the colon.
Once we’ve entered the loop, we print the value of the variable n , then we add 1 to its value. Now n is 2, and we go back to test our while statement. This is still true, since 2 < 5 , so we run through the next two lines of code again… And we keep on with this pattern while the statement n < 5 is true .
As soon as this statement becomes false , we’re completely done with the loop; we jump straight to the end and continue on with the rest of the script, in this case printing out “Loop finished”.
Notice the indentation on the lines after the colon. The colon and indenting let Python know that the next lines are inside the while loop. As soon as Python sees a line that isn’t indented, that line and the rest of the code after it will be considered outside of the loop.
The second type of loop, a for loop, is slightly different in Python. We typically use for loops in Python in order to loop over every individual item in a set of similar things -these things could be numbers, variables, lines from an input file, etc.
For instance, the following code does the exact same thing as our previous script by using a for loop to repeatedly run code for a range of numbers:
for n in range(1, 5):
print(“n =”, n)
print(“Loop finished”)
Here we are using the range() function, which is a function that is built into Python, to return a list of numbers. The range() function takes two input values, a starting number and a stopping number. So in the first line, we create a variable n equal to 1, then we run through the code inside of our loop for n = 1 .
We then run through the loop again for n = 2 , etc., all the way through our range of numbers. Once we get to the end of the range, we jump out of the loop. In Python a range(x, y) starts at x but ends right before y.
NOTE:
When we use a for loop, we don’t need to define the variable. This is because the for loop reassigns a new value to the variable each time we run through the loop. In the above example, we assigned the value 1 to the object n as soon as we started the loop. This is different from a while loop, where the first thing we do is to test some condition — which is why we need to have given a value to any variable that appears in the while loop before we enter the loop.
As long as we indent the code correctly, we can even put loops inside loops.
Try this out:
for n in range(1, 4):
for j in [“a”, “b”, “c”]:
print(“n =”, n, “and j =”, j)
Here j is looping over a list of strings.This example is only to show that you don’t have to use the range() function with a for loop. Since the j loop is inside the n loop, we run through the entire j loop ( j=”a”, j=”b”, j=”c” ) for each value that gets assigned to n in the outer loop.
