


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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.
A pictorial representation of if-else block is given below : Python If Else Statement
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
if
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.
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.
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