🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed. In this example, the condition for while will be True as long as the counter variable ...
Published   December 23, 2025
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
February 13, 2024
Do while loop in python
I am trying to implement the queue data structure in Python. I want to make the program function according to user-input. Like, the user should be able to decide if they want to enqueue or dequeue elements, or display elements in the queue. I was wondering if there is an alternative to the ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
January 26, 2021
python - While loop example - Stack Overflow
x = y // 2 # For some y > 1 while x > 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') This ... More on stackoverflow.com
🌐 stackoverflow.com
As a beginner how do I understand while loops?
It’s just a loop which continues running until the condition becomes false. More on reddit.com
🌐 r/learnpython
76
36
April 10, 2025
🌐
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!
🌐
LearnPython.com
learnpython.com › blog › python-while-loop-example
8 Python while Loop Examples for Beginners | LearnPython.com
February 5, 2024 - The expression scores[keys[i]] gives us the value of the key given by keys[i]. For example, the first item in the keys list is Jane and scores['Jane'] returns the value associated with Jane in the scores dictionary, which is 88.
🌐
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.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
i = 0 while i < 10: print(i) if i == 6: break i = i + 1 print('All done') 0 1 2 3 4 5 6 All done · Here is a more realistic example of break looping over a string. The while loop goes through the index numbers in the usual way 0, 1, 2, .. len-1. In the loop, an if statement checks for a digit, and breaks out of the loop when found.
🌐
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...
Find elsewhere
🌐
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…
🌐
Wikipedia
en.wikipedia.org › wiki › Python_(programming_language)
Python (programming language) - Wikipedia
1 day ago - The for statement, which iterates over an iterable object, capturing each element to a variable for use by the attached block; the variable is not deleted when the loop finishes · The while statement, which executes a block of code as long as boolean condition is true · The try statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses (or new syntax except* in Python 3.11 for exception groups); the try statement also ensures that clean-up code in a finally block is always run regardless of how the block exits
🌐
Python Tutor
pythontutor.com › visualize.html
Python Tutor - Visualize Code Execution
In this example, the user would see their custom LinkedList data structure getting incrementally built up one Node at a time via recursive calls to init() until the base case is reached when n==0. Despite its name, Python Tutor is also a widely-used web-based visualizer for Java that helps students to understand and debug their code.
🌐
Substack
ebpfchirp.substack.com › p › all-the-ways-to-loop-and-iterate
All The Ways To Loop and Iterate in eBPF Coding Lab Now Available
4 weeks ago - Yes, loops—like for and while in Python 😅 · It might sound trivial, but there are actually 5+ ways to iterate and loop in eBPF, each with its own pros and cons. In this week’s lab, you will learn all about them—and, most importantly, give them a try in code.
🌐
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.
Top answer
1 of 8
4

This is a lame primality test.

% is the mod operator. It performs division and returns the remainder rather than the result of the division. For example, 5 // 2 == 2, and 5 % 2 == 1.

Commented:

x = y // 2  # For some y > 1  ##Reduce search space to half of y
while x > 1:
  if y % x == 0: # Remainder  ##If x divides y cleanly (4 / 2 == 2)
    print(y, 'has factor', x) ##y is not prime
    break  # Skip else        ##Exit the loop
  x -= 1   # Normal exit  ##Try the next value
else:
  print(y, 'is prime')
2 of 8
1

The program prints at least one factor of an integer y, or if it has no factors (other than itself and 1), prints that y is prime.

It uses the variable x to try all possible factors greater than one. It starts at the floor of y divided by 2, because no number larger than half of y could be a factor. Using normal division rather than floor division could give you a fractional value if y is odd. (An even better solution is to start with the square root of y - if y is not prime, one of its factors will be less than or equal to its square root.)

Inside the loop, it tests y % x, which is the remainder after dividing y by x. If the remainder is zero, that means that x is a factor of y, and it prints it.

The else clause is executed at the end of the loop, unless a factor is found, in which case the "break" skips out of the loop and the else clause. So either a factor is found, or it's prime.

Here's the improved code with the indentation fixed:

import math

def check_primality(y):
  x = int(math.sqrt(y))
  while x > 1:
    if y % x == 0:                                                
      print y, 'has factor', x
      break
    x -= 1
  else:
    print y, 'is prime'
🌐
Jinja
jinja.palletsprojects.com › en › stable › templates
Template Designer Documentation — Jinja Documentation (3.1.x)
String literals in templates with ... native Python strings are not safe. A control structure refers to all those things that control the flow of a program - conditionals (i.e. if/elif/else), for-loops, as well as things like macros and blocks. With the default syntax, control structures appear inside {% ... %} blocks. Loop over each item in a sequence. For example, to display ...
🌐
Indiachinainstitute
indiachinainstitute.org › wp-content › uploads › 2018 › 05 › Python-Crash-Course.pdf pdf
Python crash course
Using a while Loop with Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
🌐
Tutorials
zframez.com › tutorials › chapter 6: python while and for loops: examples and explanations
Chapter 6: Python While and For Loops: Examples and Explanations - Tutorials
October 16, 2024 - For example, if the input is 1234, the output should be 4321. Write a Python program to print all perfect numbers between 1 and 100. (A perfect number is a number whose divisors sum up to the number itself, e.g., 6 = 1 + 2 + 3.) Write a Python program to find the greatest common divisor (GCD) ...