Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Explore the while Loop in Python | Loops in Python
Introduction to Python (dev copy)

bookExplore the while Loop in Python

Often, in programming, you want your code to run repeatedly as long as a specific condition is true. Think about how, in everyday life, we stay on a subway train until we reach our designated stop. If our destination is "Station B," we'll pass by "Station A," "Station C," and so forth until we arrive at "Station B." In Python, you can simulate this behavior with a while loop, structured like this:

while condition:
  do_something

For instance, we can use this loop to print all numbers up to 10.

1234567
# Assign starting number (counter) i = 1 # While loop will print all the numbers to 10 while i < 10: # Condition print(i, end = ' ') # Action i = i + 1 # Increasing variable
copy

Note

By default, the print() function outputs each result on a new line. By employing the end=' ' argument, we ensure that multiple print() outputs are separated by a space. We'll be using this technique throughout this section.

The loop's logic is outlined above. You might observe that we've included i = i + 1 within the loop. Without this line, our loop would run indefinitely because each time the condition is checked, it would find 1 < 10, which is always True. So, when working with while loops, it's crucial to ensure your code doesn't enter an endless loop.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Ställ mig frågor om detta ämne

Sammanfatta detta kapitel

Visa verkliga exempel

Awesome!

Completion rate improved to 1.64

bookExplore the while Loop in Python

Svep för att visa menyn

Often, in programming, you want your code to run repeatedly as long as a specific condition is true. Think about how, in everyday life, we stay on a subway train until we reach our designated stop. If our destination is "Station B," we'll pass by "Station A," "Station C," and so forth until we arrive at "Station B." In Python, you can simulate this behavior with a while loop, structured like this:

while condition:
  do_something

For instance, we can use this loop to print all numbers up to 10.

1234567
# Assign starting number (counter) i = 1 # While loop will print all the numbers to 10 while i < 10: # Condition print(i, end = ' ') # Action i = i + 1 # Increasing variable
copy

Note

By default, the print() function outputs each result on a new line. By employing the end=' ' argument, we ensure that multiple print() outputs are separated by a space. We'll be using this technique throughout this section.

The loop's logic is outlined above. You might observe that we've included i = i + 1 within the loop. Without this line, our loop would run indefinitely because each time the condition is checked, it would find 1 < 10, which is always True. So, when working with while loops, it's crucial to ensure your code doesn't enter an endless loop.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 1
some-alt