Course Content
Python Loops Tutorial
Python Loops Tutorial
The First while Loop
The while
loop is used to repeat a block of code as long as a specified condition evaluates to True
. The condition is checked at the beginning of each iteration, and the loop stops when the condition becomes False
.
condition
: a Boolean expression that evaluates to True
or False
.
We will print all destinations from the travel_list
one by one.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Print each destination using a while loop while i < len(travel_list): print(travel_list[i]) i += 1
How does the code work?
- The variable
i
starts at0
, which represents the first index of thetravel_list
; - The while loop checks if
i
is less than the length of the list (len(travel_list)
). This ensures the loop doesn't exceed the list bounds; - The loop prints the destination at the current index
travel_list[i]
; - The variable
i
is incremented by1
in each iteration usingi += 1
; - When
i
becomes equal to the length of the list, the condition evaluates toFalse
, and the loop stops.
Swipe to show code editor
Write a program using a while loop to:
- Print all cities from the travel_list until you encounter the city
"Barcelona"
. - Stop printing as soon as
"Barcelona"
is found, without printing it.
Thanks for your feedback!
The First while Loop
The while
loop is used to repeat a block of code as long as a specified condition evaluates to True
. The condition is checked at the beginning of each iteration, and the loop stops when the condition becomes False
.
condition
: a Boolean expression that evaluates to True
or False
.
We will print all destinations from the travel_list
one by one.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Print each destination using a while loop while i < len(travel_list): print(travel_list[i]) i += 1
How does the code work?
- The variable
i
starts at0
, which represents the first index of thetravel_list
; - The while loop checks if
i
is less than the length of the list (len(travel_list)
). This ensures the loop doesn't exceed the list bounds; - The loop prints the destination at the current index
travel_list[i]
; - The variable
i
is incremented by1
in each iteration usingi += 1
; - When
i
becomes equal to the length of the list, the condition evaluates toFalse
, and the loop stops.
Swipe to show code editor
Write a program using a while loop to:
- Print all cities from the travel_list until you encounter the city
"Barcelona"
. - Stop printing as soon as
"Barcelona"
is found, without printing it.
Thanks for your feedback!
The First while Loop
The while
loop is used to repeat a block of code as long as a specified condition evaluates to True
. The condition is checked at the beginning of each iteration, and the loop stops when the condition becomes False
.
condition
: a Boolean expression that evaluates to True
or False
.
We will print all destinations from the travel_list
one by one.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Print each destination using a while loop while i < len(travel_list): print(travel_list[i]) i += 1
How does the code work?
- The variable
i
starts at0
, which represents the first index of thetravel_list
; - The while loop checks if
i
is less than the length of the list (len(travel_list)
). This ensures the loop doesn't exceed the list bounds; - The loop prints the destination at the current index
travel_list[i]
; - The variable
i
is incremented by1
in each iteration usingi += 1
; - When
i
becomes equal to the length of the list, the condition evaluates toFalse
, and the loop stops.
Swipe to show code editor
Write a program using a while loop to:
- Print all cities from the travel_list until you encounter the city
"Barcelona"
. - Stop printing as soon as
"Barcelona"
is found, without printing it.
Thanks for your feedback!
The while
loop is used to repeat a block of code as long as a specified condition evaluates to True
. The condition is checked at the beginning of each iteration, and the loop stops when the condition becomes False
.
condition
: a Boolean expression that evaluates to True
or False
.
We will print all destinations from the travel_list
one by one.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize the index i = 0 # Print each destination using a while loop while i < len(travel_list): print(travel_list[i]) i += 1
How does the code work?
- The variable
i
starts at0
, which represents the first index of thetravel_list
; - The while loop checks if
i
is less than the length of the list (len(travel_list)
). This ensures the loop doesn't exceed the list bounds; - The loop prints the destination at the current index
travel_list[i]
; - The variable
i
is incremented by1
in each iteration usingi += 1
; - When
i
becomes equal to the length of the list, the condition evaluates toFalse
, and the loop stops.
Swipe to show code editor
Write a program using a while loop to:
- Print all cities from the travel_list until you encounter the city
"Barcelona"
. - Stop printing as soon as
"Barcelona"
is found, without printing it.