Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Iterating with range() Function | The For Loop
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
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:

  • 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): # Indices: 0, 1, 2 print(travel_list[i])
copy
  1. range(3) generates numbers from 0 to 2 (end value 3 is exclusive);
  2. Inside the loop, travel_list[i] accesses the destination at index i.

Let's print the travel destinations in reverse order:

12345
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing travel destinations in reverse for i in range(len(travel_list) - 1, -1, -1): # Start at last index, end at -1, step -1 print(travel_list[i])
copy
  1. len(travel_list) calculates the number of items in the list (5 in this case);
  2. range(len(travel_list) - 1, -1, -1) generates indices from 4 to 0;
  3. The loop accesses travel_list[i] in reverse order and prints:
Task

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 4, while your friends will visit the rest.

  • Fill the your_travel_list with the indexes of countries that are divisible by 4.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

book
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:

  • 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): # Indices: 0, 1, 2 print(travel_list[i])
copy
  1. range(3) generates numbers from 0 to 2 (end value 3 is exclusive);
  2. Inside the loop, travel_list[i] accesses the destination at index i.

Let's print the travel destinations in reverse order:

12345
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing travel destinations in reverse for i in range(len(travel_list) - 1, -1, -1): # Start at last index, end at -1, step -1 print(travel_list[i])
copy
  1. len(travel_list) calculates the number of items in the list (5 in this case);
  2. range(len(travel_list) - 1, -1, -1) generates indices from 4 to 0;
  3. The loop accesses travel_list[i] in reverse order and prints:
Task

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 4, while your friends will visit the rest.

  • Fill the your_travel_list with the indexes of countries that are divisible by 4.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt