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

book
The First for Loop

In computer programming, a loop is a series of instructions that continuously repeats until a specific condition is met. Loops are essential for automating repetitive tasks and efficiently processing data.

We'll begin our exploration of Python loops with the for loop.

Syntax of the for Loop

  • 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;
  • The indented block of code below the for statement is executed for every item in the sequence.

Example: Printing Letters of a Travel Destination

Imagine we have the name of a city, "Sydney", and we want to print each letter of the city's name in a column. Here's how we can do this using a for loop:

12345
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
copy

Explanation:

  • The variable city holds the string "Sydney";
  • The for loop iterates over each character in the string;
  • On each iteration, the variable letter takes the value of the next character in the string;
  • The print(letter) statement outputs the current character to the console.

This demonstrates how the for loop processes each character individually and performs the same operation—in this case, printing the character.

Task
test

Swipe to show code editor

Let's take this further! Imagine you have a list of cities you want to visit. Write a for loop to print each city's name on a new line:

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

In computer programming, a loop is a series of instructions that continuously repeats until a specific condition is met. Loops are essential for automating repetitive tasks and efficiently processing data.

We'll begin our exploration of Python loops with the for loop.

Syntax of the for Loop

  • 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;
  • The indented block of code below the for statement is executed for every item in the sequence.

Example: Printing Letters of a Travel Destination

Imagine we have the name of a city, "Sydney", and we want to print each letter of the city's name in a column. Here's how we can do this using a for loop:

12345
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
copy

Explanation:

  • The variable city holds the string "Sydney";
  • The for loop iterates over each character in the string;
  • On each iteration, the variable letter takes the value of the next character in the string;
  • The print(letter) statement outputs the current character to the console.

This demonstrates how the for loop processes each character individually and performs the same operation—in this case, printing the character.

Task
test

Swipe to show code editor

Let's take this further! Imagine you have a list of cities you want to visit. Write a for loop to print each city's name on a new line:

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