All types of loops in Python with examples

In this tutorial, we will learn about all types of loops in Python.

In Python, there are three types of loops to handle the looping requirement.

if and else statement

1. If statement: In Python, if condition is used to verify whether the condition is true or not. If condition is true execute the body part or block of code. If false doesn’t execute the body part or block of code.

Syntax:

if condition:
      body part 0r block of code

Example:

# if statement example
name = 'Alice'
if name == 'Alice': #Checks the statement
    print("Hi Alice")

Output:

Hi Alice

Flowchart:
if and else statement

1.1 if and else statement: If condition checks the conditions, if it is True, execute the if block code i.e body1 or if condition is False, execute the else block code i.e body2.

Syntax:

if condition:
       body1
else:
       body2

Example:

# if statement example
name = 'Alice'
if name == 'Horseman': 
    print("Hi, Alice")
#else statement
else:
    print("Hello, Stranger")

Output:

Hello, Stranger

Flowchart:
if and else statement

1.2. Elif statement: In the previous statement, we can check only two conditions i.e if or else. But sometimes we require many conditions to check, so here comes elif condition statements.

Syntax:

if condition:
      body
elif condition:
      body2
elif condition:
      body3
else:
      body4

Example:

# if statement example
name = 'Alice'
age = 10
if name == 'Horseman':
    print("Hi, Alice")
#elif statement
elif age < 12:
    print("You are not Alice, kiddo")
elif age > 2000:
    print("Unlike you, Alice is not an undead, immortal vampire")
elif age > 100:
    print("You are not Alice, grannie")

Output:

You are not Alice, kiddo

Flowchart:
Elif statement

while loop statement

2. while loop statement: In while loop condition, the block of code execute over and over again until the condition is True. If condition gets False it doesn’t execute the block.

Syntax:

while condition:
         body

Example:

# while statement
spam = 0
while spam < 5:
    print("Hello, World")
    spam = spam + 1

Output:

Hello, World
Hello, World
Hello, World
Hello, World
Hello, World

Flowchart:

while loop statement

for loop statement

3. for loop statement: The while loop keeps execute while its condition is True. But what if you want to execute the code at a certain number of times or certain range. This you can do using for loop and range function.

Syntax:

for value in sequence:
            body

Example:

# for loop statement
print("My name is")
for i in range(5):
    print("Jimmy Five Times ("+str(i)+")")

Output:

My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)

Flowchart:

for loop statement

Related posts:

While loop in Python

How to break out of multiple loops in Python?

Break and Continue Statement in Python

How to find the system time in Python

numpy.squeeze() in Python

Leave a Reply

Your email address will not be published. Required fields are marked *