Nearby lessons

42 of 108

Python - Flow Control

Introduction

Flow Control decides the order in which Python statements are executed during program execution.

It helps the program make decisions, repeat tasks, and control the execution flow.

Types of Flow Control

Category Statements
Conditional Statements if, if-elif, if-elif-else
Transfer Statements break, continue, pass
Iterative Statements for, while

Conditional Statements

Conditional statements execute different blocks of code based on a condition.

Python provides the following conditional statements:

  • if
  • if-elif
  • if-elif-else

1. if Statement

The if statement executes a block of code only when the condition is True.

Syntax:

if condition:
    statement
        

Or

if condition:
    statement1
    statement2
    statement3
        

Important Note

  • If the condition is True, all statements inside the if block are executed.
  • If the condition is False, the complete block is skipped.

Example 1 - if Statement

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Name: Durga
Hello Durga Good Morning

Output 2

Enter Name: Ravi

(No output because the condition is False.)

Example 2 - Favourite Brand

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Your Favourite Brand: RC
It is Childrens Brand

Output 2

Enter Your Favourite Brand: KF

(No output because the condition is False.)

if with Multiple Statements

An if block can contain multiple statements.

Example - Multiple Statements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Enter Name: Durga

Hello Durga
Good Morning
How are you?

2. if-else Statement

The if-else statement is used when there are two possible actions.

  • If the condition is True, the if block is executed.
  • If the condition is False, the else block is executed.

Syntax:

if condition:
    Action1
else:
    Action2
        

Example 1 - if-else

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Name: Durga
Hello Durga Good Morning

Output 2

Enter Name: Ravi
Hello Guest Good Morning

Example 2 - Biggest of Two Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter First Number: 10
Enter Second Number: 20

Biggest Number is: 20

Output 2

Enter First Number: 30
Enter Second Number: 15

Biggest Number is: 30

Conditional Statements Summary

Statement Description
if Executes a block only when the condition is True.
if-else Executes one of two blocks.

Important Points

  • Flow Control decides the order of statement execution.
  • Python has Conditional, Transfer, and Iterative statements.
  • The if statement executes only when the condition is True.
  • The if-else statement chooses one of two blocks.
  • Indentation is compulsory in Python.

3. if-elif Statement

When multiple conditions need to be checked, use the if-elif statement.

Python checks conditions from top to bottom.

Once a condition becomes True, the corresponding block is executed and the remaining conditions are skipped.

Syntax

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Example - if-elif

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Your Favourite Brand: KF

It is not that much kick

Output 2

Enter Your Favourite Brand: FO

Buy One Get Two Free

Important Note

Only one matching block is executed. After that, all remaining conditions are skipped.

4. if-elif-else Statement

The else block executes when none of the above conditions are True.

Syntax

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Example - if-elif-else

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Your Favourite Brand: RC

It is Children's Brand

Output 2

Enter Your Favourite Brand: ABC

Other Brands are not Recommended

Program - Biggest of Three Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 30

Biggest Number is: 30

Output 2

Enter First Number: 100
Enter Second Number: 20
Enter Third Number: 30

Biggest Number is: 100

Program - Smallest of Three Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 5

Smallest Number is: 5

Program - Even or Odd Number

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Number: 10

Even Number

Output 2

Enter Number: 15

Odd Number

Program - Check Number Between 1 and 100

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Number: 50

The Number is between 1 and 100

Output 2

Enter Number: 150

The Number is not between 1 and 100

Program - Print Digit in Words

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Enter Digit From 0 to 9: 7

SEVEN

Output 2

Enter Digit From 0 to 9: 15

Please Enter Digit From 0 to 9

Quick Summary

Statement Description
ifExecutes when the condition is True.
if-elseExecutes one of two blocks.
if-elifExecutes one block from multiple conditions.
if-elif-elseExecutes one matching block or the else block.

Important Notes

  • elif means else if.
  • Use elif to check multiple conditions.
  • Only the first matching condition is executed.
  • The else block is optional.
  • If no condition is True, the else block executes.
  • Indentation is compulsory in Python.

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Looping Statement