Tutorial Playlist

Python Tutorial for Beginners

Overview

How to Install Python on Windows?

Lesson - 1

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

Lesson - 2

Getting Started With Jupyter Network

Lesson - 3

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 4

A Beginner’s Guide To Python Variables

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Learn A to Z About Python Functions

Lesson - 7

The Basics of Python Loops

Lesson - 8

Introduction to Python While Loop

Lesson - 9

Python For Loops Explained With Examples

Lesson - 10

Understanding Python If-Else Statement

Lesson - 11

Introduction to Python Strings

Lesson - 12

Everything You Need to Know About Python Slicing

Lesson - 13

All You Need To Know About Python List

Lesson - 14

Python Regular Expression (RegEX)

Lesson - 15

An Introduction to Python Threading

Lesson - 16

Objects and Classes in Python: Create, Modify and Delete

Lesson - 17

The Best Python Pandas Tutorial

Lesson - 18

A Beginner's Guide To Web Scraping With Python

Lesson - 19

A Handy Guide to Python Tuples

Lesson - 20

How to Easily Implement Python Sets and Dictionaries

Lesson - 21

Everything You Need to Know About Python Arrays

Lesson - 22

An Introduction to Matplotlib for Beginners

Lesson - 23

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 24

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 25

The Best NumPy Tutorial for Beginners

Lesson - 26

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 27

Top 10 Reason Why You Should Learn Python

Lesson - 28

How To Become a Python Developer

Lesson - 29

Top 50 Python Interview Questions and Answers in 2020

Lesson - 30
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.

Syntax: WHILE EXPRESSION:

STATEMENT(S)

Flowchart:

operation

Fig: Operation of Python while loop

Example:

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.

Example:

break-statement.

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.

Example:

continue-statement

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.

Example:

with-else

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.

Solution:

solution-1

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

SimplilearnSimplilearn

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.