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.
while condition:
# Code to execute while the condition is True
condition: a Boolean expression that evaluates to True or False.
We will print all destinations from the travel_list one by one.
123456789travel_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
- The variable
istarts at0, which represents the first index of thetravel_list; - The while loop checks if
iis 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
iis incremented by1in each iteration usingi += 1; - When
ibecomes equal to the length of the list, the condition evaluates toFalse, and the loop stops.
Swipe to start coding
You are a traveler planning your next adventure. To make things manageable, you decide to visit only half of the countries from a given list.
- Use a while loop to go through the list, selecting only the first half of the destinations.
- Use
//for integer division to correctly determine the halfway point of the list for thewhileloop condition. - Store your chosen countries in the
selectedlist.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain what would happen if we forgot to increment `i` inside the loop?
What is the output of this code?
How can I modify the loop to print the destinations in reverse order?
Awesome!
Completion rate improved to 5
The First while Loop
Swipe to show menu
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.
while condition:
# Code to execute while the condition is True
condition: a Boolean expression that evaluates to True or False.
We will print all destinations from the travel_list one by one.
123456789travel_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
- The variable
istarts at0, which represents the first index of thetravel_list; - The while loop checks if
iis 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
iis incremented by1in each iteration usingi += 1; - When
ibecomes equal to the length of the list, the condition evaluates toFalse, and the loop stops.
Swipe to start coding
You are a traveler planning your next adventure. To make things manageable, you decide to visit only half of the countries from a given list.
- Use a while loop to go through the list, selecting only the first half of the destinations.
- Use
//for integer division to correctly determine the halfway point of the list for thewhileloop condition. - Store your chosen countries in the
selectedlist.
Solution
Thanks for your feedback!
single