Even though our last article concentrated on working with Lists in your Python code, at the very end we introduced you to a For Loop in Python which allowed us to move through all the values in the List.
We only briefly touched on the subject, so this article is going to move a lot further in depth and show you how you can use Loops within your Python code. Python has two main types of Loops you can use, they are :
If we take the code we used in our previous article we had the List called total_savings. We used the For Loop to go through each of the values in the list assigning these values to the variable of "i". The second line of the Loop is the statement that is performed over each of the items, in this instance adding the value to the total variable.
for i in total_savings:
total += int(i)
To see some more examples, open the interactive shell in Python and see how the following For Loops work. In the first example, we have created the variable named different_fruits which we have added a list of different fruit names. The loop starts by assigning each of the values in the List to the x variable, and then simply uses the print() function to output each of the values to the screen:
>>> different_fruits = ["apples", "bananas", "cherrys"]
>>> for x in different_fruits:
... print(x)
...
apples
bananas
cherrys
The following examples, shows that we don't even need to be using a list to create a For Loop. In each of the examples, we have added what is going to be looped through in the "for" statement of the Loop. As you can see in the first example, we have simple added the String "fruits" which then outputs each of the letters in the word. The second example then using the range() function. The range function simply provides a List of numbers from 1 to what ever the number in the brackets, in our example 10. The Loop then outputs all the details below:
>>> for x in "fruits":
... print(x)
...
f
r
u
i
t
s
>>> for x in range(10):
... print(x)
...
0
1
2
3
4
5
6
7
8
9
If you ever have a situation where you are still trying to decide what your code will include as the statement in your For Loop. You need to make sure it does something. In the example below, which adds a simple List directly into the first line of the For Loop, but as you can see the statement being run is the "pass" statement, which doesn't do anything but does not cause an error in your code:
>>> for x in [1, 2, 3]:
... pass
...
>>> orders= ["4", "8", "2"]
>>> different_fruits = ["apples", "bananas", "cherrys"]
>>>
>>> for x in orders:
... for y in different_fruits:
... print("Please order {} boxes of {}".format(x,y))
...
Please order 4 boxes of apples
Please order 4 boxes of bananas
Please order 4 boxes of cherrys
Please order 8 boxes of apples
Please order 8 boxes of bananas
Please order 8 boxes of cherrys
Please order 2 boxes of apples
Please order 2 boxes of bananas
Please order 2 boxes of cherrys
>>> i = 1
>>> while i < 6:
... print(i)
... i += 1
...
1
2
3
4
5
NOTE: The main way to use Continue and Break is by using an If Statement in Python to test a certain condition. We haven't shown you If Statements yet but will only be using the basic functionality for now.
The best way to show you how this is done is to use some examples. The first example we have below uses the Break statement. First we use the "if x == "n": statement to test if the letter is equal to "n". If this equals to true, then the break statement is used. As you can see from the output below the whole Loop then stops completely. The output only loops through the "b" and "a" of the Loop:
>>> for x in "banana":
... if x == "n":
... break
... print(x)
...
b
a
Below we have a Continue statement where instead of only running the Loop for the first two characters, every character is run except for the letter "n".
>>> for x in "banana":
... if x == "n":
... continue
... print(x)
...
b
a
a
a
This has been a bit of a quicker article but hopefully we've been able to make it clear how Loops can be used in Python. We'll keep going with our sample program in our next article and also provide a clear introduction to If Statements.
Found this post useful? Kindly tap the up vote button below! :)