Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Exploring Steps and Reverse with range() | 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
Challenge: Exploring Steps and Reverse with range()

The range() function generates a sequence of numbers and is often used in loops for controlled iteration. Its syntax is:

  • start: The number where the sequence begins (default is 0);
  • end: The number where the sequence stops (not included);
  • step: The increment (or decrement) between numbers (default is 1).
Task
test

Swipe to show code editor

  1. Write a program to print every second city from the travel list (use step = 2 in range()).
  2. Write a program to print the last three cities in reverse order.

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 3
toggle bottom row

book
Challenge: Exploring Steps and Reverse with range()

The range() function generates a sequence of numbers and is often used in loops for controlled iteration. Its syntax is:

  • start: The number where the sequence begins (default is 0);
  • end: The number where the sequence stops (not included);
  • step: The increment (or decrement) between numbers (default is 1).
Task
test

Swipe to show code editor

  1. Write a program to print every second city from the travel list (use step = 2 in range()).
  2. Write a program to print the last three cities in reverse order.

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 3
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