Best post on looping in python (while loop)-Part 2




Looping statement in python 

In programming there will be some statements or group of statements which we want to execute continuously for more than one time, this is where loops comes in the picture.
Looping allow us to continuously execute particular statement or block of statement to more than one time.
Looping will check for the condition, and will continuously execute particular statement or block of statement until the condition get false or invalid.
Repeated execution of a set of statements is called as iteration.
This statements are executed in order except when a jump statements are encountered.

Following are the types of loops in python programming.
1. for loop
2. for else loop
3. while loop
4. while-else loop
5. Nested loop
6. Infinite loop

Looping statements in python
<strong>Looping statements in python</strong>

while loop

In python while loop is used to iterate over a sequence like list,string, tuple etc and other iterable objects.
Iterating over a sequence is called as traversal.
We can also embed conditional statements in for loop.
Syntax
Here is the simple syntax for looping statement in python : while loop−

while expression:
    statement(s)

In this statements can be single or multiple i.e block of statement with uniform indent.
The condition or expression may be Logical expression.
The condition will return True for non – zero value and the loop will iterate continuously till the condition remains True.
If the conditions turns to be False then the control will be shifted directly to the set of lines present immediately after the loop.
As python uses indentation to group statements just as curly braces in other programming languages, all statements in code black will be indented uniformly to ensure that they are part of same code block.

while loop
<strong>while loop in python (itvoyagers.in)</strong>

Explanation
If the logical expression turns out to be False than the entire set of code assigned inside that block will be skipped.
And if the condition is True than the loop is executed and corresponding set of code also gets executed.
We can say that the while loop might not run even once in case of False logical expression.

Example of while loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program to show use of while loop in python """

counter=0
while counter < 10:
    print("count is",counter)
    counter+=1

Output

Output of while loop in python
<strong>Output of while loop in python</strong>

Single Statement Suites

Just as we write if statement , we can also write while statement with its executable statement in while header if its executable block consist of single line.

Example of one line while loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program to show use of one line while loop in python """

num=10
while num < 20: num = num + 1; print("num is",num)

Output

Output of one line while loop in python
<strong>Output of one line while loop in python</strong>

while-else loop

Python allows us to use else statement with loop statement.
In while loop we can use else statement.
In while loop if an else statement is used then the else block will be executed only if the expression or condition in while statements returns False.

Example of while else loop statement

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program to show use of while else loop  """

print("Output of while else statement")
num=0
while num < 10:
    print(num, "is less than 10")
    num+=1
else:
    print(num, "is not less than 10")

Output

Output of while else loop
<strong>Output of while else loop </strong>

More examples of while loop

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program to show use of while loop  """

num=1
while num < 10:
    num = num + 1
    print("num is",num**3)

Output

Output of while statement
Output

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program to show use of while else loop  """

num=8
while num < 10:
    num = num + 1
    print("num is",num**3)
else:
    print("condition not satisfied Loop finished")

Output

Output of while else statement
Output

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment