EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Python 3 While Loop

Secondary Sidebar
Python 3 Tutorial
  • Python 3 Tutorial
    • Python 3 Commands
    • Python 3 Array
    • Python 3 Operators
    • Python 3 NumPy
    • Python 3 Webserver
    • Python 3 yield
    • Python 3 Pip
    • Python 3 Install
    • Python 3 raw_input
    • Python 3 HTTP Server
    • Python 3 Threading
    • Python 3 Requests
    • Python 3 Module Index
    • Python 3 String Format
    • Python 3 Unicode
    • Python 3 GUI
    • Python 3 xrange
    • Python 3 Tuple
    • Python 3 input
    • Python 3 JSON
    • Python 3 string
    • Python 3 try-except
    • Python 3 RegEx
    • Python 3 Object-Oriented Programming
    • Python 3 zip
    • Python 3 Exception
    • Python 3 write to file
    • Python 3 Functions
    • Python 3 List Function
    • Python 3 While Loop
    • Python 3 Global Variable
    • Python 3 String Methods
    • Python 3 interpreter
    • Python 3 REPL
    • Python 3 else if
    • Python 3 basics
    • Python 3 cheat sheet
    • Python 3 Print
    • Python 3 For Loop
    • Python 3 range
    • Python 3 Dictionary
    • Python 3 has_key
    • Python 3 URLlib
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 While Loop

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.

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

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
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Python Certifications Training Program (40 Courses, 13+ Projects) Learn More