EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 While Loop
 

Python 3 While Loop

Updated March 30, 2023

Python 3 While Loop

 

 

Introduction to Python 3 While Loop

Python 3 While Loop is a programming construct that is used to repeat a set of statements until a condition is met. The program code line after while loop is performed when the condition becomes false. Indefinite iteration is the category in which loop falls. Multiple numbers of times while loop is run and it was not set expressly in advance, which is known as indefinite iteration.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is Python 3 While Loop?

  • In the Python programming language, while loop statement is executing the statement of target repeatedly as long as the specific condition is true.
  • In programming, loops are used to repeat specified blocks of code. In Python, while loop iterates through code block to test expression.
  • At the time we don’t know how many times we’ll iterate ahead of time, we’ll utilize this loop.
  • After using the while loop in python expression of the program is first checked. Only if the true value is evaluated by test expression is the body of the loop entered. The expression of the test is verified again after one cycle.

Python 3 While Loop Statement

Typically, the controlling expression, expr>, includes more than one or one variable is initialized before the loop starts and subsequently updated anywhere in the loop body. At the time while loop in python 3 met, the variable expr> is evaluated in a Boolean context first. The body is then re-executed if expr> is still true.

This continues until expr> is set to false, at which point the program moves on after the loop body. Python does a condition check first. If it’s False, the loop will end and control will be handed in the loop body. The loop body is run if the condition is true, if the condition is False then the condition is checked again. As long as the condition is true, this will continue. The loop ends while the condition was false, and control is handed to the following sentence.

Below syntax shows how while statement we are using in python code:

Syntax:

Expression of while
    Statement (s)

After a programming construct, statements are regarded to be part of a single block of code, which is referred to as a statement. The expr is then tested again, and if it is still true, the body is run once more, and so on until the expression is no longer true. A statement consistent indent may be used in this case. Python’s approach for grouping statements is called indentation. When a while loop is run, expr in a Boolean context. When the condition is false, the program control is passed to the next line in the loop. All statements after a programming construct are considered part of a single block of code in Python. Python’s approach for grouping statements is called indentation.

The below example shows python 3 while loop statement as follows:

Example:

Code:

py = 11
while py < 21:
    print(py)
    py += 1

Output:

Python 3 While Loop 1

In the above example, we can see that the number is printed from 11 to 20. We have used increment value as 1 so the value is incremented by 1. At the time using value as 2 the pointer will be incremented with 2 positions.

Below example shows that the pointer is incremented with 2 positions while using incrementer as 2.

Example:

Code:

py = 1
while py < 6:
    print(py)
    py += 2

Output:

Python 3 While Loop 2

Initially, py is set to 1. The expression n < 6, which is true, hence the loop body runs. Py is incremented by 2 and then written inside the loop body. Program execution returns to line 2 at the start of the loop, where the expression is re-evaluated. Because it is still true, the body executes once more, and the number 5 is printed. At the time given condition is True, a while loop executes a piece of code. It will continue to execute the required collection code until the condition no longer holds true. Before running, a while loop always checks the condition. The loop will execute the code within the loop’s body.

Python 3 While Loop Flowchart

In the below flowchart, we can see that the expression will handle by using conditions.

flowchart

The while loop’s important feature here is that it may or may not ever run. The loop body is skipped and the first line after the while loop is executed. In the above figure, we can see that after starting execution of code it will go to see the statement expression and check the condition. If the expression condition is false interpreter will directly go outside of the loop. If the expression condition is true it will execute the statement within the while statement.

Examples of Python 3 While Loop

Given below are the examples mentioned:

Example #1

Below is the example of python 3 while loop. Below example shows while loop with single string.

Code:

py = 0
while (py < 10):
    py = py + 1
    print("Python 3 while loop")

Output:

Python 3 While Loop 4

In the above example, we can see “Python 3 while loop” statement will be executed 10 times because the while loop is executed 10 times in the condition which was we have used in code.

Example #2

The below example shows a while loop with the list. We also use a list at the time using the while loop in python.

Code:

py = [15, 29, 38]
while py:
    print(py.pop())

Output:

with list

Example #3

Below example shows while loop with continue statement is as follows.

Code:

py = 0
py1 = 'Python 3 while loop'
while py < len(py1):
    if py1[py] == 'l' or py1[py] == 'n':
        py += 1
        continue
    print('Letter is :', py1[py])
    py += 1

Output:

Python 3 While Loop 6

Example #4

The below example shows python 3 while loop using break statement are as follows.

Code:

py = 0
py1 = 'Python 3 while loop'
while py < len(py1):
    if py1[py] == 'e' or py1[py] == '3':
        py += 1
        break
    print('Letter is :', py1[py])
    py += 1

Output:

using break statement

Conclusion

Python’s approach for grouping statements is called indentation. When a while loop is run, expr in a Boolean context, and then the loop body is run if it is true. Python 3 while loop is a programming construct that is used to repeat a set of statements until a condition is met.

Recommended Articles

This is a guide to Python 3 While Loop. Here we discuss the introduction, python 3 while loop statement, flowchart, and examples. You may also have a look at the following articles to learn more –

  1. Python User Defined Exception
  2. Python Reduce
  3. Timsort Python
  4. Python Z Test

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW