Loop tipsSlide solved ✓
Loop tips and tricks
This lesson covers various useful topics related to loops, including:
- nested loops
- constructing and returning a list
The range built-in
range can be used to create a list of successive numbers. For
range(2) == [0, 1] range(3) == [0, 1, 2] range(5) == [0, 1, 2, 3, 4]
Try it below.
range function is often used together with a for loop. Here is an
for i in range(5): print(i)
Try using range
range with a for loop to print the numbers from 0 to 30.
Note that the argument given to
range is not included in the returned list.
So you will need
Optional arguments to range
You can give two arguments to
range instead of one to start at a different
0. For example:
range(1, 3) == [1, 2] range(2, 5) == [2, 3, 4] range(-5, 0) == [-5, -4, -3, -2, -1]
Use range again
range with a for loop to print the numbers from 10 to 20.
You can embed one loop into another. This is called nesting the loops. The code below is an example.
You can nest loops as deep as you'd like, but you will rarely need more than three levels of nesting.
Mixing nested loops
You can mix loop types while nesting loops. For instance, the program below keeps adding the numbers 1 through 10 until the result becomes at least 1000.
Why nest loops?
Nested loops might seem theoretical at first, but they do come up in practice.
Here are some examples where nested loops are needed:
for month in year: for day in month: calculate_wages(month, day) for model in phone_models: for screen_size in screen_sizes(model): ensure_website_works(screen_size) total = 0 for country in countries: for city in country: total = total + population(city)
Try nesting loops
The code below defines two lists:
Your task is to combine pairs of colors and nouns to create possible .com domains. Your program should print each domain on a new line.
That is, your program should output:
bluelaptop.com bluewebsite.com bluephone.com ...
And so on. In more detail:
- use a for loop to iterate over each color in
- inside the outer loop, use a for loop to iterate over each noun in
- inside the inner loop, form a domain name by concatenating the color with
the noun with the string
- that is, compute
color + noun + ".com"
- that is, compute
- print the concatenated string inside the inner loop
Appending to a list
You can add an item to the end of a list. This is called to append the item to the list.
You can append to a list by creating a new list with a single element and concatenating it with the first list. For example:
result = [1, 2] result = result +  print(result)
Another way to append
There is a different way to append to a list: you can call the
function on the list by using a dot:
You haven't learned about dots yet, or why this works, but you will soon. Until then, here's an example:
result = [1, 2] result.append(3) print(result)
Constructing a list gradually
Once you can append to a list, you can construct a list gradually and return it.
The code below is an example. It defines a function named
that takes as input a list of numbers. The function multiplies each item in
the input list by
10 and returns the result.
Modify the function so that it is named
multiply_by_20 and multiplies each
item by 20 instead of 10.
Write a function named
append_dotcom that takes a list of strings as its
append_dotcom function should return a copy of the input list, except
that each string should have
".com" appended to it.
append_dotcom(["a", "b"]) == ["a.com", "b.com"]
(Hint: start by saving an empty list,
, into a variable. Use a
to loop over each string in the input list. Use
+ to concatenate
".com" to each string. Append the new string to your list. After the loop,
return the list you built up.)