Course Content
Python Loops Tutorial
Python Loops Tutorial
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:
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
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.
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:
Thanks for your feedback!
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:
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
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.
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:
Thanks for your feedback!
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:
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
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.
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:
Thanks for your feedback!
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:
city = 'Sydney' # Printing every letter in the city's name for letter in city: print(letter)
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.
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: