0% found this document useful (0 votes)
5 views

UNIT - 2 (Python) E-Notes

The document provides an overview of flow control in Python, focusing on decision controls such as if, if-else, and ladder if-else statements. It includes examples demonstrating how to implement these controls for various applications, including finding the greatest number, calculating roots of a quadratic equation, and computing an electricity bill. Additionally, it covers loop controls, specifically while and for loops, with examples for printing multiplication tables and Fibonacci sequences.

Uploaded by

harsharma7011
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

UNIT - 2 (Python) E-Notes

The document provides an overview of flow control in Python, focusing on decision controls such as if, if-else, and ladder if-else statements. It includes examples demonstrating how to implement these controls for various applications, including finding the greatest number, calculating roots of a quadratic equation, and computing an electricity bill. Additionally, it covers loop controls, specifically while and for loops, with examples for printing multiplication tables and Fibonacci sequences.

Uploaded by

harsharma7011
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Training On Python

UNIT– 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 java to calculate electricity bill. Take number of units consumed by user and based
on units calculate the bill. The parameters are given below:-

Unit range Charge per unit


1-150 2.40 Rs./Unit
For next 151-300 3.00 Rs./Unit
For next more than 300 3.20Rs./Unit
Example Application – 4 (cont..)
# Electricity Bill Calculator
unit=int(input("Enter no. of units consumed : "))
if unit<=150:
bill=unit*2.40
elif unit>150 and unit<=300:
bill=(150*2.40)+(unit-150)*3.00
else:
bill=(150*2.40)+(150*3.00)+(unit-300)*3.20
print("Your bill=",bill)
O/P:-
Enter no. of units consumed : 200
Your bill=510.0
Example Application – 5 (Temperature Convertor)
#Temperature Convertor
print("Enter 1 for c to f")
print("Enter 2 for f to c")
ch=int(input())
if ch==1:
c=float(input("Enter temperature in c : "))
f=(9*c)/5+32
print("Temperature in f=",f)
elif ch==2:
f=float(input("Enter temperature in f : "))
c=(f-32)*5/9
print("Temperature in c=",c)
else:
print("Invalid choice")
Loop Controls In Python
Loop Controls In Python
If you have a block of code which you want to execute repeatedly then you can use a loop control. In
Python programming language there are two types of loop controls.

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:-

Initialization of loop counter


while(condition):
#code
Updation of loop counter
Example Application - 6
#WAP in python to print the table of given number
n=int(input("Enter a number : "))
i=1
while(i<=10):
print(n,"*",i,"=",(n*i))
i=i+1
Example Application - 7
#WAP a program in python to print fibonacci sequence up to n terms
n=int(input("How many terms you want in fibonacci sequence? "))
n1=0 #First term
n2=1 #Second term
print("Fibonacci Sequence : ")
print(n1,n2,end=" ")
i=1
while(i<=n-2):
n3=n1+n2
print(n3,end=" ")
n1=n2
n2=n3
i=i+1
Traverse The Chars Of A String Using For loop
#WAP to traverse the chars of given string using for loop
import time
string="SOFTPRO"
for c in string:
print(c,end="")
time.sleep(0.5)

You might also like