How to Use For, While, and Nested Loops in Python

Python supports three types of Loop statements. Those are While, For and Nested Loop. Loops also called iterations.


In my previous posts, I have added an example of how to Calculate the Area of Circle and Factorial ‘n’.

FOR, WHILE and NESTED LOOPS in Python and their syntax.

For example, to display (factorial n), you need a logic to execute as a loop and to print factorial n values.

Python Loops

  1. WHILE Loop
  2. Nested Loop
  3. For Loop

1. While Loop Flow Chart

python while loop

While Loop Enters When Test Condition is True

    Python WHILE Loop Syntax

    while test_condition:
            body of loop

    Infinite Loops

    A loop becomes an infinite loop if a condition never becomes FALSE.

    The programmer must be cautious when using while loops because of the possibility that the test_condition never evaluates to a FALSE value

    2. Nested WHILE and FOR Loops

    Python Nested While Loop

    while test_condition:
          while test_condition:
          statement(s)
          statement(s)

    Python Nested For Loop

    for val in sequence:
            for val in sequence:
            statement(s)
            statement(s)

    In the above, WHILE and FOR nested-loops, you can find a Loop within the Loop.

    3. FOR Loop Flow Chart

    Python While Loop

    It checks items in the List. If Found, executes.

    Python FOR Loop Syntax

    for val in sequence:
            statement(s)

    Related Posts

    Author: Srini

    Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.