Python While Loops
Python While Loops
Python While Loops
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python HOME
Python Intro
Python Get Started
i = 1
while i < 6:
print(i)
i += 1
Try it Yourself »
COLOR PICKER
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.
Example
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Try it Yourself »
ADVERTISEMENT
Example ADVERTISEMENT
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Try it Yourself »
Example
Print a message once the condition is false:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Try it Yourself »
Exercise:
Print i as long as i is less than 6.
i = 1
i < 6
print(i)
i += 1
Submit Answer »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.