Tutorial Playlist

Python Tutorial for Beginners


The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2022: Choosing The Best One

Lesson - 3

A Beginner’s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

Python OOPs Concept: Here's What You Need to Know

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

The Best Guide for RPA Using Python

Lesson - 34

Comprehending Web Development With PHP vs. Python

Lesson - 35

The Best Tips for Learning Python - REMOVE

Lesson - 36

The Best Way to Learn About Box and Whisker Plot

Lesson - 37

An Interesting Guide to Visualizing Data Using Python Seaborn

Lesson - 38

The Complete Guide to Data Visualization in Python

Lesson - 39

Everything You Need to Know About Game Designing With Pygame in Python

Lesson - 40

The Complete Simplified Guide to Python Bokeh

Lesson - 41

Top 150 Python Interview Questions and Answers for 2022

Lesson - 42

The Supreme Guide to Understand the Workings of CPython

Lesson - 43
Introduction to Python While Loop

Python is an object-oriented programming language that is used for application development, web development, and data analytics. Python is the most popular programming language right now partly because it’s free and open-source, has a vast standard library, and can be easily integrated with other programming languages. Among other components, Python consists of three main types of loops

  1. While loops
  2. For loops
  3. Nested loops 

This article covers Python programming's fundamental looping statements, i.e., Python while loops. We’ll provide an overview of Python while loops, including break statements, continue statements, and while loops with else—closing with a Python while loop exercise. 

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Is Python While Loop?

A While loop is used to repeat a section of code an unknown number of times until a specific condition is met.

Shown below is the syntax of a Python while loop.





Fig: Operation of Python while loop



Fig: Python While loop

For the above program, we assigned a value to the variable x as 1. While the value of the x is less than 5, it is going to print the value of x; after each iteration, the value of x will be increased by 1.

Break Statement in While Loop

The Python break statement is used to exit from the loop immediately after a particular condition is met.



Fig: Break statement

In the above statement, the while loop executes until x becomes 3. As soon as the value of x becomes equal to 3, the loop breaks.

Continue Statement in While Loop

The function of the continue statement is to skip the current iteration of a loop and continue with the next one.



Fig: Continue statement

The following loop goes until the x=3. When x becomes 3, the current iteration stops and a new iteration starts. 

While Loop With Else

Python allows an else clause at the end of a while loop. The else part is executed if the loop terminates naturally.



Fig: While loop with else

The following code executes as follows. We assign the value to variable x=1. Till x is less than 5, the loop will continue and print the value. As soon as the value of x reaches 4, the else condition is executed. 

Python While Loop Exercise

We would suggest attempting the following question on your own before you refer to the solutions.

Question: Write a program to add all the numbers less than 10 using while loop.



Fig: Solution 1

Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django. Check out Simplilearn's Python Training course.

Learn Python and Take Your Career to the Next Level

Understanding Python loops is one of the many steps toward mastering this exciting and versatile programming language. As big data and business intelligence become more broadly adopted across all industries, mastering Python could open doors in your professional career. Simplilearn’s Python Training Course is a great introduction to this dynamic programming language.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.