Hi friends, Today we will discuss about the python control structures.
- introduction to programming – Day 1
- Introduction to Algorithm – Day 2
- Python variable and operators
- python interview questions
In programming , typically the instructions are performed one by one or line by line. But there may be situations when all the statements in a program are not performed. Parts of the program which change the flow of instructions or in other word, change the flow of control are called as control structures.


Selection Control Structure in Python

Selection control is used for Decision making, branching between two or more paths.
Basically, selection control structure is used for braking the sequential flow.
Examples:
- If – else
- if – else and else if
- Switch case(Not supported)
Python If – Else


If statement
In Python, if statement evaluates the test expression inside the parenthesis. If the test expression is evaluated to true, then the statements inside the body of if is executed. If the test expression is evaluated to false then the statements inside the body of if is skipped and the control goes to else or elif block.
Else statement
If the If condition fails then the control goes to else statement and the body inside else indentation is executed.
if expression:
statements
else:
statements
If – else example:
x=10
y=20
if x > y :
print(" X is bigger ")
else :
print(" Y is bigger ")
output
Y is bigger
If with elif
if expression:
statements
elif expression:
statements
else:
statements
if – else if example:
x=500
if x > 500 :
print(" X is greater than 500 ")
elif x < 500 :
print(" X is less than 500 ")
elif x == 500 :
print(" X is 500 ")
else :
print(" X is not a number ")
output
X is 500
Guess the output.
luggage_weight=30 weight_limit=30 #Weight limit for the airline extra_luggage_charge=0 if(luggage_weight>0 and luggage_weight<=weight_limit): print("Check-in cleared") elif(luggage_weight<=(weight_limit+10)): extra_luggage_charge=300*(luggage_weight-weight_limit) else: extra_luggage_charge=500*(luggage_weight-weight_limit) if(extra_luggage_charge>0): print("Extra luggage charge is Rs.", extra_luggage_charge) print("Please make the payment to clear check-in")
Nested if-else
In some conditions we have to place one condition inside another condition statement.
An if statement within another if statement is known as nested if statement. Similarly, any decision logic can be written within an else statement also.
if condition:
if condition:
statements
else:
statements
else:
statements
Example:
mark = 72 if mark > 50: if mark > = 80: print ("You got A Grade !!") elif mark > =60 and mark < 80 : print ("You got B Grade !!") else: print ("You got C Grade !!") else: print("You failed!!")
Output: You got B Grade!!
switch case is not supported by python.
Guess the output
code 1:
num1=100 num2=200 num3=6 if(5>=num3): if(num1>100 or num2>150): print("1") elif(num1>=100 and num2>150): print("2") else: print("3")
options : 1,2 or 3 ?
code 2:
if((num1/num2==5) and (num1+num2)>5): print("1") elif((num1-num2)<=1 or (num1%num2)==0): print("2") else: print("3")
Options:
num1=11, num2=2
num1=0, num2=5
num1=5, num2=1
num1=-10,num2=2
code 3:
a = -10 b = -200 c = 2000 d = 4000 if( a*b >=d): if(d>c): if(d%c!=0): print(11) else: print(22) else: if(b/a >0): if(a<b or d%c!=0): print(33) else: print(44)
Python Iteration Control Structure(Loops)
In iteration control structure, a condition is given and the code inside the body executes until the certain condition is reached.
Python While loop

Syntax for while loop:
initialization;
while(condition)
{
//Code block to execute something
}
Example:
x=0
while(x < =5):
print(x)
x+=1
Output:
0
1
2
3
4
5
Example:
baggage_count=100 no_of_baggage_picked=10 while(baggage_count>0): no_of_baggage_picked = (int)(input ("Number of baggage:")) baggage_count = baggage_count - no_of_baggage_picked print("No. of baggage remaining:",baggage_count)
For loops
In python, for-loop allows the loop to run over a specific sequence of values. In other words, for every value in the sequence, the loop runs once. Thus we can avoid infinite loops by using a for loop.

Example:
for number in 1,2,3,4,5:
print("The current number is ",number)
Example:
for number in range(1,5): print ("The current number is ",number) print("---------------------------") for number in range(1,7,2): print ("The current number is ",number) print("---------------------------") for number in range(5,0,-1): print ("The current number is ",number)
Output:
The current number is 1
The current number is 2
The current number is 3
The current number is 4
---------------------------
The current number is 1
The current number is 3
The current number is 5
---------------------------
The current number is 5
The current number is 4
The current number is 3
The current number is 2
The current number is 1
Nested loops
Example:
number_of_passengers=5
number_of_baggage=2
security_check=True
for passenger_count in range(1, number_of_passengers+1):
for baggage_count in range(1,number_of_baggage+1):
if(security_check==True):
print("Security check of passenger:", passenger_count, "-- baggage:", baggage_count,"baggage cleared")
else:
print("Security check of passenger:", passenger_count, "-- baggage:", baggage_count,"baggage not cleared")
number_of_passengers=5 number_of_baggage=2 security_check=True for passenger_count in range(1, number_of_passengers+1): for baggage_count in range(1,number_of_baggage+1): if(security_check==True): print("Security check of passenger:", passenger_count, "-- baggage:", baggage_count,"baggage cleared") else: print("Security check of passenger:", passenger_count, "-- baggage:", baggage_count,"baggage not cleared")
When we want to stop a loop or break away from it we can use break statement.
Example:
for passenger in "A","A", "FC", "C", "FA", "SP", "A", "A": if(passenger=="FC" or passenger=="FA"): print("No check required") continue if(passenger=="SP"): print("Declare emergency in the airport") break if(passenger=="A" or passenger=="C"): print("Proceed with normal security check") print("Check the person") print("Check for cabin baggage")
When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use continue statement.
for passenger in "A","A", "FC", "C", "FA", "SP", "A", "A": if(passenger=="FC" or passenger=="FA"): print("No check required") continue if(passenger=="SP"): print("Declare emergency in the airport") break if(passenger=="A" or passenger=="C"): print("Proceed with normal security check") print("Check the person") print("Check for cabin baggage")

Pass is used to pass the code body. pass transfers the control to the next statement.
Some best websites to practice
What are the two categories of Python loops?
1.while loop
2. For loop
What is Range function in Python?
range() function is a built in python function.IT is used when a user needs to perform a task between two ranges. Ex. range(3,8)
What does Len () do in Python?
len() function is used to return the length of the given item.
How to write while loop (syntax) in python?
syntax:
while(condition):
//code
what is the syntax of for loop in python?
syntax:
for condition:
//code
What are decision control structure?
selection control structure is also called decision control structure.
1. if- else
2. if – elif – else
Conclusion
In this chapter we have learnt about python control structures.
2. Iteration control structure
Our team works effortlessly to provide you the better content.
Please share your feedback
Please share with your friends if you find it useful.
Thank you very much 🙂


GIPHY App Key not set. Please check settings