Nearby lessons

37 of 159

Python - Flow Control

📌 What You Will Learn
  • Define flow control and its role in program execution
  • Identify the three types of flow control statements
  • Distinguish if, if-else, if-elif and if-elif-else conditional statements
  • Write programs that find the biggest and smallest of three numbers
  • Convert a digit to its word form using an if-elif-else ladder

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

🐍Code Cell
1name = input("Enter Name: ")
2 
3if name == "Durga":
4 print("Hello Durga Good Morning")
Output
1

Enter Name: Durga
Hello Durga Good Morning

Output 2

Enter Name: Ravi

(No output because the condition is False.)

Example 2 - Favourite Brand

🐍Code Cell
1brand = input("Enter Your Favourite Brand: ")
2 
3if brand == "RC":
4 print("It is Childrens Brand")
Output
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

🐍Code Cell
1name = input("Enter Name: ")
2 
3if name == "Durga":
4 print("Hello Durga")
5 print("Good Morning")
6 print("How are you?")
Output
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

🐍Code Cell
1name = input("Enter Name: ")
2 
3if name == "Durga":
4 print("Hello Durga Good Morning")
5else:
6 print("Hello Guest Good Morning")
Output
1

Enter Name: Durga
Hello Durga Good Morning

Output 2

Enter Name: Ravi
Hello Guest Good Morning

Example 2 - Biggest of Two Numbers

🐍Code Cell
1a = int(input("Enter First Number: "))
2b = int(input("Enter Second Number: "))
3 
4if a > b:
5 print("Biggest Number is:", a)
6else:
7 print("Biggest Number is:", b)
Output
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

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

🐍Code Cell
1if condition1:
2 statement1
3elif condition2:
4 statement2
5elif condition3:
6 statement3
Output
No output captured.

Example - if-elif

🐍Code Cell
1brand = input("Enter Your Favourite Brand: ")
2 
3if brand == "RC":
4 print("It is Children's Brand")
5elif brand == "KF":
6 print("It is not that much kick")
7elif brand == "KO":
8 print("Buy One Get One Free")
9elif brand == "FO":
10 print("Buy One Get Two Free")
Output
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

🐍Code Cell
1if condition1:
2 statement1
3elif condition2:
4 statement2
5else:
6 statement3
Output
No output captured.

Example - if-elif-else

🐍Code Cell
1brand = input("Enter Your Favourite Brand: ")
2 
3if brand == "RC":
4 print("It is Children's Brand")
5elif brand == "KF":
6 print("It is not that much kick")
7elif brand == "KO":
8 print("Buy One Get One Free")
9elif brand == "FO":
10 print("Buy One Get Two Free")
11else:
12 print("Other Brands are not Recommended")
Output
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

🐍Code Cell
1a = int(input("Enter First Number: "))
2b = int(input("Enter Second Number: "))
3c = int(input("Enter Third Number: "))
4 
5if a > b and a > c:
6 print("Biggest Number is:", a)
7elif b > c:
8 print("Biggest Number is:", b)
9else:
10 print("Biggest Number is:", c)
Output
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

🐍Code Cell
1a = int(input("Enter First Number: "))
2b = int(input("Enter Second Number: "))
3c = int(input("Enter Third Number: "))
4 
5if a < b and a < c:
6 print("Smallest Number is:", a)
7elif b < c:
8 print("Smallest Number is:", b)
9else:
10 print("Smallest Number is:", c)
Output
Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 5

Smallest Number is: 5

Program - Even or Odd Number

🐍Code Cell
1n = int(input("Enter Number: "))
2 
3if n % 2 == 0:
4 print("Even Number")
5else:
6 print("Odd Number")
Output
1

Enter Number: 10

Even Number

Output 2

Enter Number: 15

Odd Number

Program - Check Number Between 1 and 100

🐍Code Cell
1n = int(input("Enter Number: "))
2 
3if 1 <= n <= 100:
4 print("The Number is between 1 and 100")
5else:
6 print("The Number is not between 1 and 100")
Output
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

🐍Code Cell
1digit = int(input("Enter Digit From 0 to 9: "))
2 
3if digit == 0:
4 print("ZERO")
5elif digit == 1:
6 print("ONE")
7elif digit == 2:
8 print("TWO")
9elif digit == 3:
10 print("THREE")
11elif digit == 4:
12 print("FOUR")
13elif digit == 5:
14 print("FIVE")
15elif digit == 6:
16 print("SIX")
17elif digit == 7:
18 print("SEVEN")
19elif digit == 8:
20 print("EIGHT")
21elif digit == 9:
22 print("NINE")
23else:
24 print("Please Enter Digit From 0 to 9")
Output
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
📝 Key Takeaways
  • Flow control decides the order in which Python statements are executed
  • Conditional statements execute different blocks based on a condition
  • Only one matching block runs and the remaining conditions are skipped
  • The else block executes when none of the conditions are True
  • Python provides if, if-elif and if-elif-else conditional statements

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8