0% found this document useful (0 votes)
4 views

python10

The document discusses nested conditional statements in Python, explaining how to use if/else blocks and the elif statement for multiple conditions. It provides examples of nested conditions and the execution flow of these statements. Additionally, it highlights the optional nature of the else statement and common mistakes related to conditional statements.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python10

The document discusses nested conditional statements in Python, explaining how to use if/else blocks and the elif statement for multiple conditions. It provides examples of nested conditions and the execution flow of these statements. Additionally, it highlights the optional nature of the else statement and common mistakes related to conditional statements.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 1/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Nested Conditional Statements

Nested Conditions

The conditional block inside another if/else conditional block is called as


nested conditional block.
In the below example, Block 2 is nested conditional block and condition B is
called nested conditional statement.

Code
PYTHON

1 matches_won = int(input())
2 goals = int(input())
3 if matches_won > 8:
4 if goals > 20:
5 print("Hurray")
6 print("Winner")

Input

10
22

Output

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 2/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Hurray
Winner

Input

10
18

Output

Winner

Nested Condition in Else Block

We can also write nested conditions in Else Statement.

In the below example Block 2 is a nested conditional block.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 3/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Code
PYTHON

1 a = 2
2 b = 3
3 c = 1
4 is_a_greatest = (a > b) and (a > c)
5 if is_a_greatest:
6 print(a)
7 else:
8 is_b_greatest = (b > c)
9 if is_b_greatest:
10 print(b)

Expand

Output

Elif Statement
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 4/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Use the elif statement to have multiple conditional statements between if


and else.

The elif statement is optional.

Multiple Elif Statements

We can add any number of

elif statements after if conditional block.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 5/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Execution of Elif Statement

Python will execute the elif block whose expression evaluates to true.
If multiple

elif conditions are true, then only the first elif block which is True will be
executed.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 6/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

Optional Else Statement

Else statement is not compulsory after

if - elif statements.

Code
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 7/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

PYTHON

1 number = 5
2 is_divisible_by_10 = (number % 10 == 0)
3 is_divisible_by_5 = (number % 5 == 0)
4 if is_divisible_by_10:
5 print("Divisible by 10")
6 elif is_divisible_by_5:
7 print("Divisible by 5")
8 else:
9 print("Not Divisible by 10 or 5")

Output

Divisible by 5

Possible Mistake

Cannot write an elif statement after

else statement.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 8/9
7/27/24, 9:03 PM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=9b13ad84-b75c-4328-9cd3-0c6751ae6b4d&s_id=7944b1f6… 9/9

You might also like