Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn The First For Loop | 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
The First For Loop

Using loops you can iterate over sequences like lists, strings, or numerical ranges, they allow to process large amounts of data with minimal code.

  • item is a variable that takes the value of each element in the sequence one at a time;
  • sequence is the data you are iterating through, such as a list, string, or range;
  • for statement block is executed for every item in the sequence.

Imagine you have a string variable and want to print each letter of it in a column. Since a string is a sequence of letters, you can use a loop to achieve this.

12345
word = 'iteration' # Printing every letter in the city's name for letter in word: print(letter)
copy
  • word variable holds the string 'iteration';
  • for loop iterates over each character in the string;
  • letter takes the value of the next character in the string in each iteration;
  • print(letter) statement outputs the current character to the console.

Make sure to name the item variable meaningfully. For example, if you iterate through a list called people, the appropriate variable name should be person.

Task

Swipe to start coding

You are a traveler who keeps track of countries you want to visit in countries list and those you have already visited in visited_countries. As you travel, it becomes harder to manage everything, so you decide to automate this process.

  • Update travel_list to include only the countries from countries that are not in visited_countries.

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

book
The First For Loop

Using loops you can iterate over sequences like lists, strings, or numerical ranges, they allow to process large amounts of data with minimal code.

  • item is a variable that takes the value of each element in the sequence one at a time;
  • sequence is the data you are iterating through, such as a list, string, or range;
  • for statement block is executed for every item in the sequence.

Imagine you have a string variable and want to print each letter of it in a column. Since a string is a sequence of letters, you can use a loop to achieve this.

12345
word = 'iteration' # Printing every letter in the city's name for letter in word: print(letter)
copy
  • word variable holds the string 'iteration';
  • for loop iterates over each character in the string;
  • letter takes the value of the next character in the string in each iteration;
  • print(letter) statement outputs the current character to the console.

Make sure to name the item variable meaningfully. For example, if you iterate through a list called people, the appropriate variable name should be person.

Task

Swipe to start coding

You are a traveler who keeps track of countries you want to visit in countries list and those you have already visited in visited_countries. As you travel, it becomes harder to manage everything, so you decide to automate this process.

  • Update travel_list to include only the countries from countries that are not in visited_countries.

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