Python While Loops

ChatGPT said: A colourful cartoon-style landscape illustration shows a student sitting at a school desk, glancing at their watch with a sigh. The classroom clock on the wall shows school hours, and the student looks impatient, waiting to go home but unable to leave while class is still in session.

A while loop repeats a block of code as long as a condition is true. You can think of it as: “Keep doing this until the condition is no longer met.”

While loops are useful when you don’t know in advance how many times something should repeat, but you want it to continue until a certain situation changes.

The basic idea

This prints five lines, increasing count each time, until the condition count <= 5 becomes false.

Why while loops are useful

Unlike a for loop (which repeats a set number of times), a while loop keeps going until the condition fails. This makes it perfect for things like:

  • Waiting for the correct user input
  • Running a game until the player quits
  • Repeating a task until a result is found

Useful things to try

  • Write a loop that prints numbers from 1 to 10 using a counter.
  • Make a guessing game: loop until the user types the correct word.
  • Create a loop that counts down from 5 to 1 and then prints “Blast off!”.

Summary

  • A while loop repeats as long as its condition is true.
  • It is ideal when the number of repetitions is not known in advance.
  • Always make sure the condition will eventually become false, otherwise the loop runs forever!

Main Topic

Python Program Flow

A colourful cartoon-style landscape illustration shows a playful flowchart. On one side, a path forks into two labelled branches representing an if…else decision, while nearby a looping curved arrow shows repetition for a loop. The scene is bright and engaging, making programming concepts feel approachable and fun.

Learn how Python uses if…else decisions and loops to guide program flow, choices, and repetition.

Other Tutorials in this Topic

Landscape illustration showing Python code with class, def, and if blocks, each using pass as placeholder.

Python’s Pass Placeholder

Beginner-friendly guide to Python’s pass statement, explaining its purpose, usage, examples, and common pitfalls clearly.

A colourful cartoon-style landscape illustration shows two runners on a track. One runner stops early at a finish line with a sign labelled “break”, while another leaps over hurdles marked “continue”, symbolising Python loop controls. The background is bright and playful, making programming concepts easy to visualise.

Python Breaking from Loops

Beginner’s guide to Python loop controls: learn break and continue for smarter, more flexible repetition.

A colourful cartoon-style landscape illustration shows a cheerful monkey standing by a moving conveyor belt, happily taking pieces of fruit off as they pass. The background is bright and playful, symbolising repetition and order, like a Python for loop.

Python For Loops

Beginner’s guide to Python for loops: repeat actions, process lists, and simplify tasks with clear examples.

A colourful cartoon-style landscape illustration shows a cheerful character standing at a wooden signpost. The path splits into two clear directions, with one arrow marked if and the other marked else, symbolising decision-making in Python programming.

Python If/Else Decsion Making

Beginner’s guide to Python if…else statements: learn how programs make choices and follow different paths with examples.