🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #78

Python While Loops


Python Loops

Python has two primitive loop commands:

  • while loops
  • for loops

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

Example

i = 1
while i < 6:
  print(i)
  i += 1

Note: remember to increment i, or else the loop will continue forever.

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.


The break Statement

With the break statement we can stop the loop even if the while condition is true:

Example

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

Example

i = 0
while i < 6:
  i += 1

  if i == 3:
    continue
  print(i)

The else Statement

With the else statement we can run a block of code once when the condition no longer is true:

Example

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

Note: The else block will NOT be executed if the loop is stopped by a break statement.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python While Loops – FAQs

Quick answers about learning Python While Loops in Python.

This free note from CodingNow 2.0 explains Python While Loops in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python While Loops, is 100% free with no signup required.
With focused practice, most students grasp Python While Loops in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now