Introduction Variables Data Types Numbers Casting Strings Boolean Operators Lists Tuples Dictionaries If-Else While Loops For Loops Functions Arrays Scope Modules User Input File Handling Python Maths

Welcome to the lesson on Python While Loops

While Loops

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

Example: Print i as long as i is less than 6:

syntax
Try it out yourself

Caution: Remember to increment i, or else the loop will continue forever.

Break statement

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

syntax
Try it out yourself

Continue Statement

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

Some Examples: Continue to the next iteration if i is 3:

syntax
Try it out yourself

Else statement

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

Some Examples: Print a message once the condition is false:

syntax
Try it out yourself

End of Python While Loops

You have learned While Loops in simple terms. Let's proceed on to Quiz.

Quiz Time!
Next Lesson
Back To Top