1. For loops
Let's examine a new way of working with data structures - looping!
2. Individual comparisons
If we have a list of prices, we can individually check if the value at each index is more than 10 dollars, less than five dollars, or in between.
This becomes tedious when working with lots of values.
3. For loop syntax
We can use a for loop instead! This can be read as "For each value in a sequence, perform this action". Notice the action is indented because the line before ends with a colon.
The sequence is known as an iterable, as it can be iterated over. It can be any data structure with multiple elements.
The value is known as an iterator. Value is a placeholder, meaning we can give it any name. It's common to use the letter i as shorthand for the index number of the sequence.
4. Print individual values
We can use a for loop to print each price separately.
We do this by looping through prices then printing the individual price. Here, price is the iterator, and prices is the iterable.
Python interprets this as "Find and print the first value in prices, repeat for the second value, and so on". Hence, we see each price on a single line in the output.
5. Conditional statements in for loops
Earlier, we individually checked if a price was more than 10 dollars. We can achieve this using a for loop.
6. Conditional statements in for loops
We include an if statement inside the loop to check if the iterator, price, is more than 10.
7. Conditional statements in for loops
Afterwards, we indent again. Here, we provide an action to print "More than 10 dollars" if this condition is true.
8. Conditional statements in for loops
We then use elif, meaning that if the price is not more than 10 dollars then we check if it is less than five. If so, we print this.
9. Conditional statements in for loops
We finish the for loop with an else keyword, allowing us to print the price if the previous conditions are not met, meaning it is between five and 10 dollars.
This syntax is the same as we've seen previously, just indented inside a for loop.
10. Conditional statements in for loops
The output shows three items cost between five and 10 dollars!
11. Looping through strings
We can loop over strings too!
This code will print each character in the username string variable.
12. Looping through dictionaries
To loop through a dictionary's keys and values we can use the dot-items method. As this produces tuples of keys and values, we need to provide two iterators, one for the keys and another for the values.
13. Looping through dictionaries
If we just want the keys or values we can loop over the dot-keys or dot-values methods respectively, using a single iterator.
14. Range
We can perform other tasks with for loops, such as updating variables.
To do this, let's introduce a new built-in function called range. It takes a starting number followed by an end number. Note that the start is inclusive but the end number is not, so it will be one less than what we provide. This means we should add one to this value to get the end number we want.
Range is commonly used in Python programs and is helpful for generating or modifying values. Here, we print all values in a range from one to five, using six as the last number in the range function.
15. Building a counter
Say we want to build a counter to track visits to a website.
We create a variable called visits equal to zero.
We loop through the numbers in a range from one to eleven, where each number represents a new visit.
We can update the visits variable using the plus equals syntax, which is the same as saying the variable is equal to itself plus another value - in this case, one.
Outside of the loop, we print visits and can see it has updated to the last number in our range, ten.
16. Let's practice!
Let's loop into some exercises!