Python Flow Control Tutorial

Python Flow Control

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:


Selection Statements:

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.

if-else:

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:


#Example 1

if 100:
	 print("Hiii")
else:
	print("Hello")

Output: Hiii

Note: Every constant value treated as True by Python control by default.

Zero(0) is treated as False by control


#Example-2

if -300:
	print("Hiii")
else:
	print("Hello")

Output: Hiii


#Example-3

if 0:
	print("Hiii")
else:
	print("Hello")

Output: Hello


#Example-4

x=100
if x=500:
	print("Hiii")
else:
	print("Hello")

Output

File "<ipython-input-4-1216cc9982e3>", line 2

    if x=500:

SyntaxError: invalid syntax


#Example-5

x=100
if x==500:
	print("Hiii")

else:
	print("Hello")

Output: Hello


#Example-6

b=False
if b:
	print("Hiii")

else:
	print("Hello")

Output: Hello


#Example-7

b=False
if b==False:
	print("Hii")

else:
	print("Hello")

Output: Hii


#Example-8

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?")

Output:

Enter Name:Silan

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.

x=200

y=500

if y>x:

    pass

# having an empty if statement like this, would raise an error without the pass statement

Note:

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.

if-elif-else::

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.


#Example-9

x=300
if x>500:
	print("Hiii")
elif x==400:
	print("Hello")
elif x<200:
	print("Python")
else:
	print("JAVA")

Output: JAVA


#Example-10

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")

Output:

Enter your favourite branch EE

This is Electrical branch


#Example-11

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)

Output:

Enter the first number:30

Enter the second number:20

Enter the third number:50

largest number is: 50


About the Author



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






 PreviousNext