Python Control Flow
Control flow statements are useful when a user has to make decisions based on certain criteria or conditions. For example, if a scenario where if an employee band is more than 5, then can avail interest-free ‘car loan’ whereas employees below band 5 can available only ‘Bike loan’ from the company. Such scenarios can be handled used as a control flow structure.
if-elif-else statement:
if-else is used to evaluate a given condition and based on evaluation criteria control will flow the respective block.
Note: All the control statements follow the python indentation rule which has to be consistent across the block. The general rule is to have 4 spaces as indentation.
if(condition True): { execute if block } else: ## if above condition not true the execute this block { execute else block }
Let’s convert employee band case into if-else logic:
employee_band = 4 if (employee_band >= 5): print("Eligible for Car Loan") else: print("Eligible for Bike Loan")
Eligible for Bike Loan
employee_band = 6 if (employee_band > 5): print("Eligible for Car Loan") else: print("Eligible for Bike Loan")
Eligible for Car Loan
Let’s understand the use of if-elif-else loop:
‘elif’ is used to handle multiple logical criteria. For example, if an employee in within band 5, then the employee is eligible for a loan up to 50,000 rupees whereas if an employee is within band 5 to 8, the employee is eligible for a loan up to 80,000 rupees and beyond band 8 employee, company is providing loan up to 100,000.
Let’s convert the above criteria into if-elif-else statement:
band = 7 if (band > 5): print("Your loan eligibility is upto 50,000 inr") elif(band > 5 and band < 8): print("Your loan eligibility is upto 80,000 inr") else: print("Your loan eligibility is upto 100,000 inr")
While Loop:
While loop is used to iterate over a block of code as long as the condition inside while is True. Loop will exit as soon as it encounters a False condition.
number = 1 sum = 0 while number <= 10: sum = sum + number number = number + 1 print("Sum is:", sum)
The above block of code will add the numbers from 1 to 10 using a while loop. We have added the condition that the number should be less than or equals to 10. While the condition is true, we will keep updating the values of ‘sum’ and increment the number by 1.This loop will continue till the number is incremented to 10 and the loop will exit once the number is incremented and value becomes 11.
For Loop:
For loop is also used to iterate over a sequence of objects. Loop iterates based upon the sequence and execute the block of codes and exit the loop when iteration completes.
ages = [10,20,30,40] for age in ages: print("age is:{}".format(age))
output:
age is:10
age is:20
age is:30
age is:40
range() method is used to set the number of times the loop will run and iterate through numbers.
for count in range(5): print(count)
range() also takes a parameter to give users more control about the iteration.
for count in range(1,3): print(count)
Output:
1
2