Python while Loop

  • The while statement allows us to repeatedly execute a block of statements as long as a condition is true

  • Indentation and Colon should be respected.

Syntax :

while test_expression:
    statement(s)  must be indented

In while loop, test expression is checked first. If test_expression is evaluates True, then the statements within the body of loop executes.After one iteration, the test expression is checked again. This process will continues until the test_expression evaluates to False

Python interprets any non-zero value as True. None and 0 are interpreted as False.

Flow chart of while loop

Example :

count = 0
while (count < 9):
   print("The count is:", count)
   count = count + 1

print("Good bye!")
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

The block here, consisting of the print and increment statements, is executed repeatedly until count is no longer less than 9. With each iteration, the current value of the index count is displayed and then increased by 1.

Example :

Program to add natural numbers upto n
# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
Enter n: 10
The sum is 55

while loop with else :

Python supports to have an else statement associated with a loop statement.

  • If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

  • The while loop can be terminated with a break statement. In such case, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.


counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
    print("Inside else")
Inside loop
Inside loop
Inside loop
Inside else

Next topic is for Loop

Udemy APAC

Udemy APAC

Online Live Training

We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.

Courses Offered :

  • C Programming
  • C++ Programming
  • Data Structure
  • Core Java
  • Python
  • Java Script
  • Advance Java (J2EE)
  • Hibernate
  • Spring
  • Spring Boot
  • Data Science
  • JUnit
  • TestNg
  • Git
  • Maven
  • Automation Testing - Selenium
  • API Testing

NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.

If you have any requirements, please send them to or

Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at

(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017,, All rights reserved