For Loops (definite iteration)
Iterating iterables
For loops always start with the keyword 'for' and then an iterator followed by some condition. Here are a few examples:
example-1.py from the beginning to the end
# example-1.py
# Iterating iterables, a simple 'for' loop illustrating strings as iterables in python 3
string = 'hello world!'
for i in string:
print(i)
In this first case, we define a sting of text we poignantly called string. Then we create the 'for' loop by stating that for each index (i) inside the string, we want to print out the value of 'i'. It is common practice in coding to use 'i' as the variable that iterates through something in a loop or for an index location for something iterable. The output of this code is that each letter is looked at by the 'i' in the for loop and then printed out to a new line in the console.
example-2.py from the beginning to the end
# example-2.py
# iterating iterables, a basic example of a counting loop in python 3
word = 'banana'
count = 0
for letter in word:
if letter is 'a':
count = count + 1 # count +=1 would also work here
print(count)
3
Example
In this example, we have a more pythonic idea. The word 'letter' works the same way as the 'i' in the previous example, it's just more descriptive which helps with clarity when rereading later. See if you can work out what this program does without running it.
Other than strings you can also iterate through any type of iterable including: dictionaries, lists, tuples and sets. Here are some examples:
example-3.py from 3 to 6
# example-3.py first example
list_of_fruit = ['apple', 'banana', 'cherry']
for item in list_of_fruit:
print(item)
apple
banana
cherry
example-3.py from 8 to 11
# example-3.py second example
dictionary = {'apple':3, 'banana':5, 'cherry':20}
for entry in dictionary.values(): # you can also use .items() or .keys() to get the dictionary pairs or keys
print(entry)
3
5
20
example-3.py from 13 to the end
# example-3.py third example
dictionary = {'apple':3, 'banana':5, 'cherry':20}
for entry in dictionary.items(): # See!!
print(entry)
for entry in dictionary.keys():
print(entry)
('apple', 3)
('banana', 5)
('cherry', 20)
apple
banana
cherry
Iterating Ranges
Using the range function, we can also create 'for' loops that iterate a number of times. Consider these examples:
example-4.py from 3 to 5
# example-4.py first example
for count in range(10): # range(<stop>)
print(count)
0
1
2
3
4
5
6
7
8
9
example-4.py from 7 to 9
# example-4.py second example
for count in range(4, 10): # range(<start>, <stop>)
print(count)
example-4.py from 11 to 13
# example-4.py third example
for count in range(1, 10, 3): # range(<start>, <stop>, <increment>)
print(count)
Example
In these examples, you can see how the range function is used to set a maximum limit to the number of times the 'for' loop iterates. Notice also that if you run the first example, count never makes it to 10, the numbers 0 all the way up to 9 are printed instead.
example-4.py from 15 to 20
# example-4.py fourth example
string = 'How long is a piece of string?'
count = 0
for i in range(len(string)):
count +=1
print(count)
example-4.py from 22 to 24
# example-4.py fifth example
for i in range(sum(range(4, 10))):
print(10 * i)
Example
In these two examples, it's slightly more complicated to work out the integer after the calculation, but we know it will be a finite number. See if you can work out what these programs will print out without running them.
Nested Loops
All loops can also be nested inside each other consider this example:
example-5.py from 1 to 8
# example-5.py first example
# Nested Loops, some examples of nested for loops
print("First Example")
adjective = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adjective:
for y in fruits:
print(x, y)
Example
In this example we iterate over two separate lists and join them together in the print statement. In the outside 'for' loop, x runs through each of the items in the variable 'adjective', then the inner 'for' loop is called and y iterates through the 'fruits' variable. When the outer loop gets to the first index, it runs the inner loop until it completes all it's iterations and then x moves onto the next index. In our example this means that 'x' first has the value 'red', then 'y' iterates through each of the 'fruits' list printing out 'red apple', 'red banana' and 'red cherry' first... Run the code for yourself and see.
To see how many times an inner loop will run, you can multiply it by all the possible different options of the outer loops and the inner loop to find out. Consider this example:
example-5.py from 11 to the end
# example-5.py second example
number = ["3", "2", "1", "100"]
adjective = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
count = 0
for x in number:
for y in adjective:
for z in fruits:
count += 1
print(x, y, z)
total = len(number) * len(adjective) * len(fruits) # Multiplied as explained in the README.md
if count == total:
print("We just proved how to calculate the number of times a loop runs!!")
print(count)
else:
print("Epic fail dood!!")
If you run this code, you can see that the code runs 36 times and that that's exactly the same number of times as each of list lengths multiplied together. There are some other niche cases you may see in loops, one of which is the 'else' statement.
example-6.py from 1 to 19
# example-6.py first example
# Showing complex nested loops with else statements
number = ["3", "2", "1", "100"]
adjective = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
count = 0
for x in number:
for y in adjective:
for z in fruits:
count += 1
print(x, y, z)
else:
print("inside loop!")
else:
print("inner loop")
else:
print("outside loop!")
print(count)
print(len(number) * len(adjective) * len(fruits))
When the loop finishes, anything in the 'else' statement is executed. If the loop is broken out of or fails to execute, the 'else' statement doesn't execute. Try this for yourself:
example-6.py from 22 to 39
# example-6.py second example
# Showing broken complex nested loops with else statements
number = ["3", "2", "1", "100"]
adjective = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
count = 0
for x in number:
for y in adjective:
for z in fruits:
break
else:
print("inside loop!")
else:
print("inner loop")
else:
print("outside loop!")
print(count)
print(len(number) * len(adjective) * len(fruits))
Test Your Knowledge
Countdown
See if you can create a loop that can count down from 10, and that once it gets to zero it prints the the word 'Liftoff!'. See if you can also make it print that the rocket was successfully launched.
Square of Stars (nested loops)
Use nested loops to create a square of stars. Store the size of the star in a variable so changing this variable is all that is required to make the square larger or smaller.
Output should be something like this:
A square of size 5
*****
*****
*****
*****
*****
Triangle of stars (nested loops)
This is similar to the square above, but instead should display a triangle.
Example output:
A triangle of size 6
*
**
***
****
*****
******