Python While Loop Statement
Python Basics
Python Introduction
Python Installation
Overview of Jupyter IDE
Identifiers & Reserved Keywords
Python Variables
Python Numbers
Python Operators
Python Operators and Arithmetic Operators
Comparison and Logical Operators
Assignment and Bitwise Operators
Identity and Membership Operators
Python Flow Control
if else if else statement
While Loop Statement
Python For Loop
Break and Continue Statement
Python Data Types
Python Strings
Python Strings Methods
Python Lists
Python Tuples
Python Dictionary
Python Functions
Introduction to Python Functions
Function Arguments
Recursion Function
Lambda/Anonymous Function
Python - Modules
Python Files
Python - Files I/O
Python - Exceptions Handling
Python - Debugging
Python Loops
A loop statement allows us to execute a statement or group of statements multiple times.
Python has two primitive loop commands:
While Loop
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true
Syntax:
while expression: Body of while
# Program to print numbers in ascending order till the mentioned limit i = 1 while i <= 6: print(i) i += 1 print("Execution Completed!") Output: 1 2 3 4 5 6 Execution Completed!
Using else Statement with Loops
If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
# Program to print numbers in ascending order till the mentioned limit i = 1 while i <= 6: print(i) i += 1 else: print("Else statement is executed at the end as mandatory") print("Execution Completed!") Output: 1 2 3 4 5 6 Else statement is executed at the end as mandatory Execution Completed!