🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
For example, while True: user_input = input("Enter password: ") # terminate the loop when user enters exit if user_input == 'exit': print(f'Status: Entry Rejected') break print(f'Status: Entry Allowed') ... Before we wrap up, let’s put your knowledge of Python while loop to the test!
Discussions

Need help understanding this while loop
I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number More on discuss.python.org
🌐 discuss.python.org
0
0
February 13, 2024
Reddit
I've been writing Python scripts for a while now. Nothing crazy, just automating small stuff, scraping some data, making my life a little easier. I thought I had a decent handle on things. I was looking at someone else's code and they used a list comprehension in a way that made me stop and read it three times. I realized I had been writing loops ... More on reddit.com
🌐 r/learnpython
October 29, 2018
[Python] Why would I use a for loop instead of a while loop?

for means "do something with each of these things".

while means "keep doing this until something is no longer true".

So... say what you mean. Readability counts.

More on reddit.com
🌐 r/learnprogramming
59
49
November 23, 2011
I've trying to learn while loops for over 2 weeks... Help? [ELI5]

You have two variables, m and c. The variable m is what the user inputs to determine what height the ball is dropped from. The variable c will count the number of bounces.

Then the while loop starts. Quite literally, while the ball bounces back to a height that is greater than 0.01, your program keeps executing the rest of the code over and over.

In this specific case, your program first decreases the height to 70% of previous value (so it bounces back lower due to gravity), then increments the c counter by one, so says the ball bounces one time.

Then the instruction in the while loop is over. The program checks again if the height m is greater than 0.01. If it is, it will keep repeating the while loop code block until the condition is no longer met. If it isn't, it will do the rest of the code. In this example it will print that the ball has bounced c number of times.

You print c because that is the variable where you store number of bounces. If you printed m it would print the height the ball is at right now, after all those operations. Which would mean it would print a number that's less than 0.01.

More on reddit.com
🌐 r/learnpython
10
3
March 2, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. Python Continue Statement returns the control to the beginning of the loop. ... # Prints all letters except 'e' and 's' i = 0 a = 'geeksforgeeks' while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 continue print(a[i]) i += 1
Published   December 23, 2025
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
We do this by writing the loop as while True:... Here is an example that uses while/True to go through the numbers 0..9. This not the best way to generate those numbers; it just shows how if/break can serve instead of a while/test at the top of the loop.
🌐
LearnPython.com
learnpython.com › blog › python-while-loop-example
8 Python while Loop Examples for Beginners | LearnPython.com
February 5, 2024 - Let’s go over a simple Python while loop example to understand its structure and functionality: >>> i = 0 >>> while i < 5: >>> print(i) >>> i += 1 ... The condition in this while loop example is that the variable i must be less than 5. The ...
🌐
Python.org
discuss.python.org › python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number <= 5: print(current_number) current_number += 1 After just watching a video on Yo…
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - When you evaluate a list in a Boolean context, you get True if it contains elements and False if it’s empty. In this example, colors remains true as long as it has elements. Once you remove all the items with the .pop() method, colors becomes false, and the loop terminates. Getting user input from the command line is a common use case for while loops in Python.
Find elsewhere
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
This example uses the time.sleep() function to create a 1-second delay between each countdown number. The while loop is a versatile and powerful control structure in Python that can help you manage iterative tasks based on conditions.
🌐
Tutorialspoint
tutorialspoint.com › python › python_while_loops.htm
Python - While Loops
In Python, all the statements indented ... to be part of a single block of code. Python uses indentation as its method of grouping statements. The following flow diagram illustrates the while loop − · The following example illustrates the working of while loop...
🌐
DataCamp
datacamp.com › tutorial › python-while-loop
Python While Loops Tutorial | DataCamp
June 25, 2020 - In the below example, you initially start with an error equal to 50.0. Next, you will write a while loop, in the condition part, we write error > 1, so that the while loop executes again as long as the error is above 1. Inside the while loop, you divide the error by four and update the error ...
🌐
Alma Better
almabetter.com › bytes › tutorials › python › while-loop-in-python
While Loop in Python
October 2, 2024 - An infinite while loop continually executes in Python until a specified condition is met. For example, the Loop below will print "Hello World" repeatedly until the Loop is manually stopped.
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
Python computes n >= 1 or 0.5 >= 1, which is false so the while-loop ends with i = 4. You may have asked, “What if the logical expression is true and never changes?” and this is indeed a very good question. If the logical expression is true, and nothing in the while-loop code changes the expression, then the result is known as an infinite loop. Infinite loops run forever, or until your computer breaks or runs out of memory. EXAMPLE...
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - For example, when you're writing a program that takes in input from users you may ask for only a positive number. The code will run at least once. If the number the user submits is negative, the loop will keep on running.
🌐
BrainStation®
brainstation.io › learn › python › while-loop
Python While Loop (2026 Tutorial & Examples) | BrainStation®
February 4, 2025 - Write the logic inside the while loop that should be executed if the condition is True in step 2. The condition is always checked first and only if it’s True, the logic inside of the while loop will be executed otherwise the code that follows the while loop will be executed.
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
In this example, we're using a while loop to print the numbers from 1 to 10. However, we're also using an if statement to check if the loop has reached the halfway point, which is when i is equal to 5. If we've reached the halfway point, we ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not to use this type of loop as it is a never-ending infinite loop where the condition is always true and we have to forcefully terminate the compiler. Python programming language allows to use one loop inside another loop which is called nested loop. Following example ...
Published   1 month ago
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-1-while-loop
5.1 While loop - Introduction to Python Programming | OpenStax
March 13, 2024 - A counter variable can be used in the loop expression to determine the number of iterations executed. Ex: A programmer may want to print all even numbers between 1 and 20. The task can be done by using a counter initialized with 1.
🌐
Mimo
mimo.org › glossary › python › while-loop
Master Python While Loops: A Comprehensive Guide
Master Python from basics to advanced topics, including data structures, functions, classes, and error handling ... Start your coding journey with Python. Learn basics, data types, control flow, and more ... condition: A boolean expression that the while loop evaluates before each iteration.
🌐
freeCodeCamp
freecodecamp.org › news › while-loops-in-python-while-true-loop-statement-example
While Loops in Python – While True Loop Statement Example
July 19, 2022 - Something to note here is that the user input by default is case-sensitive, which means that if the user enters 'python' instead of 'Python' they still won't be able to continue. To fix this, you can use a string method such as .capitalize() to capitalize the first letter of the word the user enters. user_input = input("Please enter the secret keyword: ").capitalize() Next, it is time to construct the while loop.