Docsity

Python If Else and Nested If Else, Summaries of Programming Languages

Toggle sidebar
bg1
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
A pictorial representation of if-else block is given below :
Pyt ho n If El s e Sta t em en t
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
Python If Else and Nested If Else
if <condition>:
statements
else:
statements
pf3
pf4

Partial preview of the text

Download Python If Else and Nested If Else and more Summaries Programming Languages in PDF only on Docsity!

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

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

Python If Else and Nested If Else

if : statements else : statements

Output 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") 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.") The condition is false. Inside else block Statement in else block Outside if-else condition block. 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.")

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