PYTHON Notes - Control flow,loop statements
PYTHON Notes - Control flow,loop statements
Condition Description
if An if statement consists of a boolean expression followed by one or
more statements.
if-else / else-if An if statement can be followed by an optional else statement, which
executes when the boolean expression is FALSE.
nested – if You can use one if or else if statement inside another if or else
if statement(s).
If - flow chart
Example:
a = 10
b = 15
if a<b:
print("A is small number")
Note : When the condition is True program statements prints other wise no output.
Examples
Find the eligibility of an employee for Gratuity, if employee has the eligibility provide the 10% of his
salary?
Program
yos=int(input("enter a years of service"))
if yos>5:
salary = int(input("enter a salary"))
print("Gratutity is :",salary*0.10)
else:
print("No Gratutity")
Else – If
Example
Find the student grades based on their marks.
Marks 0 to 35 → Fail
Marks 35 to 60 → Grade C
Marks 60 to 75 → Grade B
Marks 75 to 100 → Grade A
Example
Program
marks=int(input("Enter Marks "))
if marks >= 0 and marks <35:
print("Fail")
elif marks >= 35 and marks <60:
print("Grade C")
elif marks >= 60 and marks < 75:
print("Grade B")
elif marks >=75 and marks <=100:
print("Grade A")
else:
print("Invalid")
Example
Generate the Electricity bill for different slabs ?
Slab 1 → 0 – 100 units → Minimum charge is 200
Slab 2 → 100 – 200 units → Per unit is 5/-
Slab 3 → 200 – 300 units → per unit is 10/-
Slab 4 → 300 and above → per unit is 15/-
Example
Electricity Bill Generation
Program
num=int(input("Enter number of electric unit"))
if num<=100:
amt=200
if num>100 and num<=200:
amt=200+(num-100)*5
if num>=200 and num<=300:
amt=700+(num-200)*10
if num>300:
amt = 1700 + (num - 300) * 15
print("Amount to pay :",amt)
Loop
The flow of the programs written in any programming language is sequential by default.
Sometimes we may need to alter the flow of the program. The execution of a specific code
may need to be repeated several numbers of times.
Why we use loops in python?
The looping simplifies the complex problems into the easy ones.
It enables us to alter the flow of the program so that instead of writing the same code again
and again, we can repeat the same code for a finite number of times
While loop
❑ While loop is used to iterate over a block of code repeatedly until a given condition
returns false.
❑ The main difference is that we use while loop when we are not certain of the number
of times the loop requires execution.
❑ On the other hand when we exactly know how many times we need to run the loop,
we use for loop.
Syntax
while condition:
#body_of_while
Flow of while loop
❑ First the given condition is checked, if the condition returns false, the loop is terminated
and the control jumps to the next statement in the program after the loop.
❑ If the condition returns true, the set of statements inside loop are executed and then
the control jumps to the beginning of the loop for next iteration.
These two steps happen repeatedly as long as the condition specified in while loop remains
true.
While loop
Program
Program 1:
n=int(input("Enter a Number : "))
while n<15:
print(n)
n+=1
❑ The for loop is used in the case where we need to execute some part of the code until
the given condition is satisfied.
❑ The for loop is also called as a per-tested loop. It is better to use for loop if the number
of iteration is known in advance.
❑ The for loop in Python is used to iterate the statements or a part of the program several
times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
Syntax :
for iterating variable in iterating sequence:
statement(s)
Examples
Program to find sum all numbers in the list?
List is [1,2,3,4,5]
Program:
numbers = [1,2,3,4,5]
sum = 0
for val in numbers:
sum = sum + val
print("The sum is", sum)