Python While Loops

Python · Loops

Theory

A while loop repeats its block as long as its condition stays truthy. Python checks the condition before every pass: if true, the body runs and control jumps back to the check; if false, the loop ends and execution continues after it. In the example, count starts at 0, is printed, and is increased by 1 each pass; when count reaches 5 the test count < 5 is False, so the output is 0 to 4. If the condition is false at the start, the body never runs at all.

The body must eventually make the condition false, usually by updating a variable. Forget count += 1 and the loop never stops: an infinite loop (stop it with Ctrl+C or the stop button in Jupyter). Deliberate infinite loops, while True:, are fine when the body uses break to exit, for example when reading input until the user types quit. break leaves the loop immediately, and continue skips to the next condition check.

Why we use it

Some repetition has no fixed count: retry until a request succeeds, read until the file ends, keep asking until the input is valid. while repeats based on a condition, not on a collection.

When to use it

Use while when you do not know in advance how many passes are needed. When looping over items of a collection or a known range, prefer for, which cannot forget to update its counter and so cannot become accidentally infinite.

How it works

  1. count = 0
  2. Is count < 5 true? Yes: Run the body. No: Exit the loop.
  3. Body: print(count)
  4. Body: count += 1 (moves toward ending the loop)
  5. Jump back to the condition check
  6. After the loop: printed 0, 1, 2, 3, 4

Syntax

while condition:
    ...

Example

count = 0
while count < 5:
    print(count)
    count += 1

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic