Lecture - 2 (Python) E-Notes
Lecture - 2 (Python) E-Notes
Lecture - 2 (Python) E-Notes
Lecture – 2
Flow Controls
Decision Control (If)
Decision Controls are used for decision making. The decision controls in python are given below:-
1. If statement
2. If – else statement
3. Ladder if – else statement
if Statement:- if is a keyword which works like decision control. We attach a condition with if statement.
If given condition is true then code will executed and if given condition is false then it do nothing.
Syntax:-
if condition:
#if statement code
Example Application -1
n=int(input("Enter a number : "))
if (n==1):
print("Hi..........")
print("Outside of if...")
O/P 1:-
Enter a number : 1
Hi………………
Outside of if……
O/P 2:-
Enter a number : 2
Outside of if…….
Decision Control (if – else)
if-else is the variation of if statement. We attach a condition with if statement. If given condition is true
then if block code will executed and if given condition is false then else block code will executed.
Syntax of if –else:-
if(Condition):
#if block code
else:
#else block code
Example Application - 2
#Develop a program in python to find greatest no in two numbers
n1=int(input("Enter first no : "))
n2=int(input("Enter second no : "))
if n1>n2:
print("Greatest No=",n1)
else:
print("Greatest No=",n2)
O/P:-
Enter first no : 10
Enter second no : 5
Greatest No=10
Example Application - 3
#WAP to find roots of quadratic equation
import math
a=float(input("Enter value of a : "))
b=float(input("Enter value of b : "))
c=float(input("Enter value of c : "))
d=(b*b)-(4*a*c)
if d<0:
print("Roots are imaginary")
else:
r1=(-b+math.sqrt(d))/(2*a)
r2=(-b+math.sqrt(d))/(2*a)
print("Root1=",r1)
print("Root2=",r2)
Decision Control (Ladder If - Else)
If you have multiple conditions and you want to execute the code based on those conditions then you can
use ladder if – else .
The syntax of if – else ladder is given below:-
if condition1:
# code1
elif condition2:
` #code2
else:
#code3
Example Application - 4
Develop a program in python to calculate electricity bill. Take number of units consumed by user and
based on units calculate the bill. The parameters are given below:-
1. While loop
2. For loop
The while loop in python is work like while loop in C, C++ and Java. It is a type of entry control. Whereas
the for loop in python is work like for each loop in java. For loop in python is used to traverse the
elements of a collection like string, list, tuple… etc.
While Loop In Python
While is a keyword in python, which works as a loop control. It is a type of entry loop control. The syntax
of while loop is given below:-