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

Python If Else and Nested If Else

The document discusses Python if-else conditional statements and nested if-else statements. It provides an example of a basic if-else statement that checks if the product of two numbers is equal to 20. It also provides an example of a nested if-else statement, where the if block contains another if-else statement and the else block contains another if-else statement. Nested if-else statements allow conditional logic to be further broken down into multiple levels. Proper indentation is important in Python to define code blocks for if, else, and nested conditional statements.

Uploaded by

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

Python If Else and Nested If Else

The document discusses Python if-else conditional statements and nested if-else statements. It provides an example of a basic if-else statement that checks if the product of two numbers is equal to 20. It also provides an example of a nested if-else statement, where the if block contains another if-else statement and the else block contains another if-else statement. Nested if-else statements allow conditional logic to be further broken down into multiple levels. Proper indentation is important in Python to define code blocks for if, else, and nested conditional statements.

Uploaded by

Jashwanth Sangu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python If Else and Nested If Else

Python If Else

Python If Else – Basic idea of an if else condition block is that, when the condition is satisfied, the statements
in the if block are executed, and when the condition is not satisfied, the statements in the else block are
executed.

Syntax – Python If-Else


if <condition>:
statements
else:
statements

A pictorial representation of if-else block is given below :

Python If Else Statement

Example 1 – Python If Else


In this example, we will write an if-else statement. The condition checks if product of two numbers is 20 or not.

example.py – Python Program


number_1 = 5
number_2 = 6
if number_1 * number_2 == 20:
print("The condition is true.")
print("Inside if block")
print("Statement in if block")
else:
print("The condition is false.")
print("Inside else block")
print("Statement in else block")
print("Outside if-else condition block.")

Output

The condition is false.


Inside else block
Statement in else block
Outside if-else condition block.

Note : Observe the indentation and alignment of the statements inside the if-block or else-block. Unlike
programming languages like java or c where the statements are enclosed in curly braces, python considers the
alignment of statements to consider them in the block.

Nested if-else
This is an extension for the idea of if-else block. Any statement in the if or else blocks could be another if or if-
else block.

Example 2 – Nested If-Else


In this example, we will write a nested if-else statement. If block contains an if-else statement inside it, and else
block also contains an if-else block inside it.

example.py – Python Program

number_1 = 5
number_2 = 6
if number_1 * number_2 == 20:
print("The condition is true.")
print("Inside if block")
if (number_1 == number_2):
print("Both numbers are same")
else:
print("Both numbers are not same")
else:
print("The if condition is false.")
print("Inside else block")
if number_1 + number_2 == 11:
print("Addition of two numbers equal 11.")
else:
print("Addition of two numbers is not equal to 11.")
print("Outside nested if-else condition block.")
Output

The if condition is false.


Inside else block
Addition of two numbers equal 11.
Outside nested if-else condition block.

Conclusion
In this Python Tutorial, we have learnt if-else conditional statement, and how any statement in if or else block
could be another if or if-else conditional statements.
Python Programming

⊩ Python Tutorial

⊩ Install Python

⊩ Install Anaconda Python

⊩ Python HelloWorld Program

⊩ Python Variables

⊩ Python Variable Data Type Conversion

⊩ Python Comments

Control Statements

⊩ Python If

⊩ Python If Else

⊩ Python While Loop

⊩ Python For Loop

Python String

⊩ Python String Methods

⊩ Python String Length

⊩ Python String Replace

⊩ Python Split String

⊩ Python Count Occurrences of Sub-String

⊩ Python Sort List of Strings

Functions

⊩ Python Functions

Python Collections

⊩ Python List

⊩ Python Dictionary

Advanced

⊩ Python Multithreading

Useful Resources

⊩ Python Interview Questions

You might also like