Python Fundamentals

While Loop in Python

To get things right with while loop in python, you need to check for loop in python  first.

The mean difference between for and while loops is :

For Loop : Executing Statements till reach defined purpose and
While Loop : Executing Statements while defined condition is true

While Loop in Python Syntax

very close to for loop syntax, let’s see

while expression_test:
   statements(s)
number_of_students = 10
attended = 0

while attended <= number_of_students:
    print(attended)
    attended += 1

while loop in python doing something while condition is true


While Loop in Python With Else

Else isn’t new for us we discussed it on our python if elif else and for loop in python , see next image for more understanding

while loop in python else

number_of_students = 10
attended = 0

while attended <= number_of_students:
    print(attended)
    attended += 1
else:
    print('Done..')

 

 

Write a Comment

Your email address will not be published. Required fields are marked *