The order in which the python program codes execute at the run time is known as python flow control. Python flow control is categorized into 3 types of statements like Selection Statements, Iterative Statements, Transfer Statements which is as follows:
In this context, among several options, one option will select and that select option will execute. if-else and if-else-if are coming under selection statements that we will discuss.
The general form is :
if b: statement1 statement2 --------- #if body statementn else statement1 statement2 --------- #else body statementn
Here b is anything that should be a boolean value(True / False).
First we will evaluate b and get whether it will be True or False. If we will get True, then if body will execute, otherwise else body will execute.
If we will not get Boolean value then error will raise.
We will see varieties of examples:
if 100: print("Hiii") else: print("Hello")
Note: Every constant value treated as True by Python control by default.
Zero(0) is treated as False by control
if -300: print("Hiii") else: print("Hello")
if 0: print("Hiii") else: print("Hello")
x=100 if x=500: print("Hiii") else: print("Hello")
File "<ipython-input-4-1216cc9982e3>", line 2
SyntaxError: invalid syntax
x=100 if x==500: print("Hiii") else: print("Hello")
b=False if b: print("Hiii") else: print("Hello")
b=False if b==False: print("Hii") else: print("Hello")
name=input("Enter Name:") name=input("Enter Name:") if name=='Silan': print("Hii very gud mrng") else: print("Hii very gud evening") print("How are you?")
Hii very gud mrng
How are you?
The pass statement
if statements cannot be empty, but if you for some reason have an
if statement with no content, put in the
pass statement to avoid getting an error.
# having an empty if statement like this, would raise an error without the pass statement
In python we have defined if statement by using if keyword and with colon and then indentation.
Indentation is nothing but whitespace which specifies the scope in the code, but in other programming languages we use curly braces instead of white spaces.
In if-else, we studied when we have a scenario having two options and among two options, one option is selecting and executing.
When more than two options coming to our picture in our scenario then we will use if-elif-else. The else if in short is called as elif, the elif condition checks for multiple options, if the condition for if statement is false then, python will check for the elif block, if the condition of all the elif is false then the body of else condition is executed.
x=300 if x>500: print("Hiii") elif x==400: print("Hello") elif x<200: print("Python") else: print("JAVA")
branch=input("Enter your favourite branch") if branch=="CSE": print("This is Software branch") elif branch=="ETC": print("This is Electronics branch") elif branch=="EE": print("This is Electrical branch") else: print("Invalid branch")
Enter your favourite branch EE
This is Electrical branch
n1=int(input("Enter the first number:")) n2=int(input("Enter the second number:")) n3=int(input("Enter the third number:")) if n1>n2 and n1>n3: print("largest number is:",n1) elif n2>n3: print("largest number is:",n2) else: print("largest number is:",n3)
Enter the first number:30
Enter the second number:20
Enter the third number:50
largest number is: 50
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc