• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » While Loop in Python
Next →
← Prev

While loop in Python

By Dinesh Thakur

A while loop in Python is a control flow statement that repeatedly executes a block of statements based on a given boolean condition. In this tutorial, you will learn how to create a while loop in Python.

We’ll be covering the following topics in this tutorial:

  • What is while loop in Python?
  • While Loops versus For Loops
  • Syntax of while Loop in Python
  • Flowchart of while Loop
  • Python – While loop example
  • Infinite while loop
  • Nested while loop in Python
  • Python – while loop with else block

What is while loop in Python?

While loop in Python uses to iterate over a block of code as long as a given expression evaluates to (boolean) “true.” The block stops execution if and only if the given condition returns to be false. The while loop can be considered as a repeating if statement. It is also known as a pre-tested loop.

We usually use this loop when we do not know how many times to iterate before.

While Loops versus For Loops

In Python, the ‘while’ and for’ loops are very similar and confusing. Remember, the for’ loop repeats for a default number of times according to the sequence’s length. On the other hand, a ‘while’ loop will continue to run as long as the condition is true.

Therefore, when a repeated operation is required, you should use the “while” loop, but you do not know beforehand how many iterations are required.

Syntax of while Loop in Python

The syntax of a while loop in Python programming language is

while expression:
  statement(s)

In the while loop, statement(s) may be a single statement or a block of statements. The loop iterates as long as the situation is true.

This process continues until the False expression evaluated, the program control immediately passes to the line after the loop.

Flowchart of while Loop

Flowchart of while Loop1. First, the conditions are checked. If the condition returns false, the loop is terminated, and the control jumps into the program’s next statement following the loop.
2. If the condition is true, the statements within the loop are executed, and then the control jumps to the start of the loop for the next iteration.
These two steps repeatedly happen as long as the condition specified in the while loop is true.

Python – While loop example

Here is an example of a while loop. In this example, we have a variable num, and we are displaying the value of num in a loop. The loop has an increase in operation in which we increase the num value. It is an extremely important step. The while loop must be increased or decreased; otherwise, the loop will run indefinitely.

The variable num initialized to 0 before the block starts. Till it is less than 5, num is incremented by one and printed to display the numbers’ sequence, as below.

num = 0
#loop will repeat itself as long as
#num < 5 remains true
while num < 5:
 print("num =", num)
 #incrementing the value of num
 num = num + 1

Output:

While loop example in PythonInfinite while loop

num = 1
while num<5:
  print(num)

Loop will print ‘1’ indefinitely because we don’t update the value of num within the loop. The value of num always stays 1, and the condition num < 5 returns true at all times.

Nested while loop in Python

If a while loop is present within a while loop, it is called a nested while loop. Let’s take an example of this concept to understand.

loop_a = 1
loop_b = 5
while loop_a < 4:
 while loop_b < 8:
   print(loop_a, ",", loop_b)
   loop_b = loop_b + 1
   loop_a = loop_a + 1
Output:

Nested while loop in PythonPython – while loop with else block

We can have an ‘else’ block linked to the while loop. The block ‘else’ is optional. It only executes after the execution of the while loop.

num = 10
while num > 5:
  print("num =", num)
  num = num - 1
else:
  print("loop is ended")
Output: while loop with else block in python

You’ll also like:

  1. What is for-loop in Python With examples?
  2. Discuss how and when the values of the loop index change throughout the processing of the loop
  3. Explain differences between Do/Loop and For-Next Loop.
  4. Python File I/O: Read and Write to Files in Python
  5. What is Python? | Introduction to Python Programming
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW