Best post on nested loop in python(looping) -Part 3




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>

Nested loop in python

Loops can be nested in Python similar to nested loops in other programming languages.
Nested loop allows us to create one loop inside another loop.
It is similar to nested conditional statements like nested if statement.
Nesting of loop can be implemented on both for loop and while loop.
We can use any loop inside loop for example, for loop can have while loop in it.

Nested for loop

For loop can hold another for loop inside it .
In above situation inside for loop will finish its execution first and the control will be returned back to outside for loop.
Syntax

Here is the simple syntax of nested for loop in python

for iterator in iterable:
    for iterator2 in iterable2:
        statement(s) of inside for loop
    statement(s) of outside for loop

In this first for loop will initiate the iteration and later second for loop will start its first iteration and till second for loop complete its all iterations the control will not be given to first for loop and statements of inside forloop will be executed.
Once all iterations of inside for loop are completed then statements of outside for loop will be executed and next iteration from first for loop will begin.

1. Example of nested for loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date  :24th August 2020 
Description : Program to show nested for loop in python """

for i in range(1,11):
    for j in range(1,11):
        m=i*j
        print(m,end=' ')
    print("Table of ",i)

Output

Output of nested for loop in python (itvoyagers.in)
<strong>Output of nested for loop in python</strong>

2. Example of nested for loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date  :24th August 2020 
Description : Program to show nested for loop in python """

for i in range(15):
    for j in range(i):
        print("*",end=' ')
    print(" ")

Output

nested for loop in python

<strong>Output of example 2: nested for loop in python</strong>




Nested while loop

While loop can hold another while loop inside it .
In above situation inside while loop will finish its execution first and the control will be returned back to outside while loop.
Syntax

Here is the simple syntax of nested while loop in python

while expression:
    while expression2:
        statement(s) of inside while loop
    statement(s) of outside while loop

In this first while loop will initiate the iteration and later second while loop will start its first iteration and till second while loop complete its all iterations the control will not be given to first while loop and statements of inside while loop will be executed.
Once all iterations of inside while loop are completed than statements of outside while loop will be executed and next iteration from first while loop will begin.

It is also possible that if first condition in while loop expression is False then second while loop will never be executed.



1. Example of nested while loop in python

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

p=1
while p<10:
    q=1
    while q<=p:
        print(p, end=" ")
        q+=1
    p+=1
    print(" ")

Output

Output of nested while loop in python (itvoyagers.in)

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




2. Example of nested while loop in python

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

x=10
while x>1:
    y=10
    while y>=x:
        print(x, end=" ")
        y-=1
    x-=1
    print(" ")

Output

nested while loop in python

<strong>Output of example 2 : nested while loop in python</strong>




Combination of nested for loop and while loop

Just as we can have for loop with in for loop and while loop within while loop, we can also have nested loops with combination of for loop with in while loop and vice versa.

Example of nested for and while loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date  :24th August 2020 
Description : Program to show nested for and while loop in python """

for i in range(15):
    j=14
    while j>=i:
        print("*",end=' ')
        j-=1
    print(" ")

Output

nested for while loop

<strong>Output of&nbsp; nested for while loop in python</strong>




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