Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
While Loop | Introduction to Python 2/2
Introduction to Python for Data Analysis
course content

Contenido del Curso

Introduction to Python for Data Analysis

Introduction to Python for Data Analysis

1. Introduction to Python 1/2
2. Introduction to Python 2/2
3. Explore Dataset
4. Becoming an Analyst

bookWhile Loop

While loop is another type of loops. It performs the same, but the construction of the loop differs.

12345678910
# Summing all prices prices = [3, 5, 6, 2, 7, 8] i = 0 counter = 0 while i < len(prices): counter += prices[i] i += 1 print(counter)
copy

When we want to stop our loop immediately, we need to use break.

123456789
prices = [1, 2, 3, 4, 5, 6] i = 0 while i < len(prices): if prices[i] == 4: break else: print(prices[i]) i += 1
copy

We use a special variable (counter in our case) to collect elements of the price list (in our case). If you want to count the sum of the numbers from 1 to 10, you also have to create a variable that collects the sum after each iteration.

For more practice with loops try this course!

Tarea

Let's count all money from the list until the sum equals the 100!

  1. Set the while loop to work with the money list.
  2. Set the condition if the counter equals 100.
  3. Finish the program if the counter equals 100.
  4. Add the money iterator to the counter.
  5. Increase the i by 1.
  6. Print the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
toggle bottom row

bookWhile Loop

While loop is another type of loops. It performs the same, but the construction of the loop differs.

12345678910
# Summing all prices prices = [3, 5, 6, 2, 7, 8] i = 0 counter = 0 while i < len(prices): counter += prices[i] i += 1 print(counter)
copy

When we want to stop our loop immediately, we need to use break.

123456789
prices = [1, 2, 3, 4, 5, 6] i = 0 while i < len(prices): if prices[i] == 4: break else: print(prices[i]) i += 1
copy

We use a special variable (counter in our case) to collect elements of the price list (in our case). If you want to count the sum of the numbers from 1 to 10, you also have to create a variable that collects the sum after each iteration.

For more practice with loops try this course!

Tarea

Let's count all money from the list until the sum equals the 100!

  1. Set the while loop to work with the money list.
  2. Set the condition if the counter equals 100.
  3. Finish the program if the counter equals 100.
  4. Add the money iterator to the counter.
  5. Increase the i by 1.
  6. Print the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
toggle bottom row

bookWhile Loop

While loop is another type of loops. It performs the same, but the construction of the loop differs.

12345678910
# Summing all prices prices = [3, 5, 6, 2, 7, 8] i = 0 counter = 0 while i < len(prices): counter += prices[i] i += 1 print(counter)
copy

When we want to stop our loop immediately, we need to use break.

123456789
prices = [1, 2, 3, 4, 5, 6] i = 0 while i < len(prices): if prices[i] == 4: break else: print(prices[i]) i += 1
copy

We use a special variable (counter in our case) to collect elements of the price list (in our case). If you want to count the sum of the numbers from 1 to 10, you also have to create a variable that collects the sum after each iteration.

For more practice with loops try this course!

Tarea

Let's count all money from the list until the sum equals the 100!

  1. Set the while loop to work with the money list.
  2. Set the condition if the counter equals 100.
  3. Finish the program if the counter equals 100.
  4. Add the money iterator to the counter.
  5. Increase the i by 1.
  6. Print the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

While loop is another type of loops. It performs the same, but the construction of the loop differs.

12345678910
# Summing all prices prices = [3, 5, 6, 2, 7, 8] i = 0 counter = 0 while i < len(prices): counter += prices[i] i += 1 print(counter)
copy

When we want to stop our loop immediately, we need to use break.

123456789
prices = [1, 2, 3, 4, 5, 6] i = 0 while i < len(prices): if prices[i] == 4: break else: print(prices[i]) i += 1
copy

We use a special variable (counter in our case) to collect elements of the price list (in our case). If you want to count the sum of the numbers from 1 to 10, you also have to create a variable that collects the sum after each iteration.

For more practice with loops try this course!

Tarea

Let's count all money from the list until the sum equals the 100!

  1. Set the while loop to work with the money list.
  2. Set the condition if the counter equals 100.
  3. Finish the program if the counter equals 100.
  4. Add the money iterator to the counter.
  5. Increase the i by 1.
  6. Print the counter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt