Conditional (Decision Making)statements:
if, if-else, if-elif-else, flowcharts, simple programs: e.g.: absolute value, sort 3 numbers and divisibility of a number
Decision Making Statements allow the program to take the decision as which statement should be executed next.
Decision Making statements are used when we want a set of instructions should be executed in one situation and different instructions should be executed in another situation .
Decision making can be implemented in python using,
1. if statements 2. if-else statements 3. if-elif-else statements 4. nested if statements
if <condition>:
statement(s)
if else statement
if <condition>:
statement(s) when condition is true
else:
statement(s) when condition is false
if elif statement
If out of multiple statements, it is required to select one statement for processing on the basis of a condition, if-elif statement is to be used.
Its syntax is-
if <condition1>:
statement(s) when condition1 is true
elif <condition2>:
statement(s) when condition2 is true
elif <condition3>:
statement(s) when condition3 is true
elif <condition4>:
statement(s) when condition4 is true
......
else:
statement(s) when all conditions become false
#Program to find given no is even or odd
n=int(input("Enter Number"))
if(n%2==0):
print("Even Number")
else :
print("Odd Number")
# Python program to check leap year or not
year=int(input(“enter a year”))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("Leap Year")
else:
print("Not a Leap Year")
else:
print("Leap Year")
else:
print("Not a Leap Year")
WAP to compute sum of digits of a given string
s='JK23 KSD 315 SD990'
sum = 0
for i in s:
if i.isdigit() == True:
z = int(i)
sum = sum + z
print(sum)
WAP to find given no is positive, negative or zero
num = int(input("Enter Number") )
if (num > 0):
print(“Number is positive”)
elif (num < 0):
print(“Number is negative”)
else:
print(“Number is Zero”)
#program for calculating electricity bill in Python
electricity charges and rates.
1 - 100 unit - 1.5/=
101-200 unit - 2.5/=
201-300 unit - 4/=
300 - 350 unit - 5/=
above 300 - fixed charge 1500/=
additional charges
if units<=100 – 25.00
if 100< units and units<=200 – 50.00
if 200 < units and units<=300 – 75.00
if 300<units and units<=350 – 100.00
if units above 350 – No additional charges
units=int(input("please enter the number of units you consumed in a month"))
if(units<=100):
payAmount=units*1.5
fixedcharge=25.00
elif(units<=200):
payAmount=(100*1.5)+(units-100)*2.5
fixedcharge=50.00
elif(units<=300):
payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4
fixedcharge=75.00
elif(units<=350):
payAmount=(100*1.5)+(200-100)*2.5+(300-200)*4+(units-300)*5
fixedcharge=100.00
else:
payAmount=0
fixedcharge=1500.00
Total=payAmount+fixedcharge;
print("\nElecticity bill=%.2f" %Total)
#program for calculating electricity bill in Python using and operator
units=int(input("Number of unit consumed: "))
if(units>0 and units<=100):
payAmount=units*1.5
fixedcharge=25.00
elif(units>100 and units<=200):
payAmount=(100*1.5)+(units-100)*2.5
fixedcharge=50.00
elif(units>200 and units<=300):
payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4
fixedcharge=50.00
elif(units>300):
payAmount=2500;#fixed rate
fixedcharge=75.00
else:
payAmount=0;
Total= payAmount+fixedcharge
print("\nElecticity bill pay=%.2f: " %Total);