Iterating with range() Function
Imagine you have a list of travel destinations and you want to work with only a specific part of it. For example, you might want to visit just the first three cities or print destinations in reverse order. Python's range() function can help you iterate over the indices of the list easily!
- The
range()function is useful when working with list indices; - It allows you to control the start, end, and step of iteration;
- Instead of iterating directly over the items, you iterate over the positions of items in a sequence.
The format of the range() function is:
range(start, end, step)
start: the starting index of the range (default is 0 if omitted);end: the ending index (exclusive);step: the increment between indices (default is 1).
Suppose we have a list of cities we want to visit. Let's print the first three destinations using range():
123456# List of travel destinations travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing the first three destinations for i in range(3): print(i, ':', travel_list[i])
range(3)generates numbers from0to2(end value3is exclusive);- Inside the loop,
travel_list[i]accesses the destination at indexi.
Let's print the travel destinations in reverse order:
123456travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing travel destinations in reverse # Start at last index, end at -1, step -1 for i in range(len(travel_list) - 1, -1, -1): print(i, ':', travel_list[i])
len(travel_list)calculates the number of items in the list (5 in this case).range(len(travel_list) - 1, -1, -1)generates indices from 4 to 0, allowing iteration in reverse order.- The loop prints each index and its corresponding destination from
travel_list, displaying the locations in reverse.
Often, we don't know the length of a list in advance. To handle this, we calculate the length of the list using the len() function.
Since Python's list indexing starts at 0, the last element's index is always len(list) - 1. For example, if a list has 5 elements, the last element is at index 4 (not 5).
Swipe to start coding
You and your friends are planning a group trip, but you decide to split up to visit different countries and cover more ground. You agree to visit the countries whose indexes are divisible by 2, while your friends will visit the rest.
- Use the
rangefunction correctly to set up the start, end, and step values. - Fill the
your_travel_listwith the indexes of countries that are divisible by 2.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5
Iterating with range() Function
Swipe to show menu
Imagine you have a list of travel destinations and you want to work with only a specific part of it. For example, you might want to visit just the first three cities or print destinations in reverse order. Python's range() function can help you iterate over the indices of the list easily!
- The
range()function is useful when working with list indices; - It allows you to control the start, end, and step of iteration;
- Instead of iterating directly over the items, you iterate over the positions of items in a sequence.
The format of the range() function is:
range(start, end, step)
start: the starting index of the range (default is 0 if omitted);end: the ending index (exclusive);step: the increment between indices (default is 1).
Suppose we have a list of cities we want to visit. Let's print the first three destinations using range():
123456# List of travel destinations travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing the first three destinations for i in range(3): print(i, ':', travel_list[i])
range(3)generates numbers from0to2(end value3is exclusive);- Inside the loop,
travel_list[i]accesses the destination at indexi.
Let's print the travel destinations in reverse order:
123456travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing travel destinations in reverse # Start at last index, end at -1, step -1 for i in range(len(travel_list) - 1, -1, -1): print(i, ':', travel_list[i])
len(travel_list)calculates the number of items in the list (5 in this case).range(len(travel_list) - 1, -1, -1)generates indices from 4 to 0, allowing iteration in reverse order.- The loop prints each index and its corresponding destination from
travel_list, displaying the locations in reverse.
Often, we don't know the length of a list in advance. To handle this, we calculate the length of the list using the len() function.
Since Python's list indexing starts at 0, the last element's index is always len(list) - 1. For example, if a list has 5 elements, the last element is at index 4 (not 5).
Swipe to start coding
You and your friends are planning a group trip, but you decide to split up to visit different countries and cover more ground. You agree to visit the countries whose indexes are divisible by 2, while your friends will visit the rest.
- Use the
rangefunction correctly to set up the start, end, and step values. - Fill the
your_travel_listwith the indexes of countries that are divisible by 2.
Solution
Thanks for your feedback!
single