Python If Else and Nested If Else - Tutorial Kart
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 : 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 ifelse 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
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- lecture 04 more iteration nested loops brown university
- evaluating nested expressions in python idc online
- list comprehensions department of computer science and electrical
- ppyytthhoonn nneesstteedd llooooppss
- chapter 3 selection statements southeastern louisiana university
- random access lists nested data types and numeral systems haskell
- lecture 6 lists nested loops and files github pages
- python if else and nested if else tutorial kart
- lecture 14 nested lists tuples and dictionaries
- about python 5 while loop