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

123456789
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
copy
  • The variable i starts at 0, which represents the first index of the travel_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 by 1 in each iteration using i += 1;
  • When i becomes equal to the length of the list, the condition evaluates to False, and the loop stops.
Task

Swipe to start coding

You are a data analyst working on a program that processes country data. Your task is to select half of the countries from the provided list. Since efficiency is key, you decide to automate this selection using a while loop.

  • Iterate through the list using a while loop, selecting only the first half of the countries.
  • Store the selected countries in the selected list.

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

book
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.

123456789
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
copy
  • The variable i starts at 0, which represents the first index of the travel_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 by 1 in each iteration using i += 1;
  • When i becomes equal to the length of the list, the condition evaluates to False, and the loop stops.
Task

Swipe to start coding

You are a data analyst working on a program that processes country data. Your task is to select half of the countries from the provided list. Since efficiency is key, you decide to automate this selection using a while loop.

  • Iterate through the list using a while loop, selecting only the first half of the countries.
  • Store the selected countries in the selected list.

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 2. 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