CH - 9 Condition Using If
CH - 9 Condition Using If
STATEMENTS
Learning Outcomes
Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
Compound Statement has:
Selection
It means execution of statement will depend
upon the condition. If the condition is true
then it will execute Action 1 otherwise
Action 2. In Python we use if to perform
selection
Selection
Action 1
True
Condition ? Statement 1 Statement 2
False
Statement 1
Action 2
Statement 2
Selection
False
Condition ? Exit from iteration
True
Statement 1
Loop Body
Python supports for and
Action 2
Statement 2
while statement for
Looping construct
Error and Exceptions
bonus = 0
sale = int(input("Enter Monthly Sales :"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " + str(bonus))
1. WAP to enter 2 number and print the largest number
2. WAP to enter 3 number and print the largest number
1. WAP to enter 2 number and print the largest number
import math
print("For quadratic equation, ax**2 + bx + c = 0,
enter coefficients ")
a = int(input("enter a"))
b = int(input("enter b"))
c = int(input("enter c"))
if a==0:
print("Value of ",a," should not be zero")
print("Aborting!!!")
else:
delta = b*b - 4 *a * c
Program to calculate and print roots of a quadratic equation:
ax2+bx+c=0(a!=0)
if delta >0:
root1=(-b+math.sqrt(delta))/(2*a)
root2=(-b-math.sqrt(delta))/(2*a)
print("Roots are Real and UnEqual")
print("Root1=",root1,"Root2=",root2)
elif delta==0:
root1=-b/(2*a)
print("Roots are Real and Equal")
print("Root1=",root1,"Root2=",root1)
else:
print("Roots are Complex and
Imaginary")
To Do…
1. WAP to enter marks of 5 subject and calculate total, percentage
and also division.
Percentage Division
>=60 First
>=45 Second
>=33 Third
otherwise Failed
2. WAP to enter any number and check it is positive, negative or zero
number
3. WAP to enter Total Bill amount and calculate discount as per given
table and also calculate Net payable amount (total bill – discount)
Total Bill Discount
>=20000 15% of Total Bill
>=15000 10% of Total Bill
>=10000 5% of Total Bill
otherwise 0 of Total Bill
To Do…
4. WAP to enter Bill amount and ask the user the payment
mode and give the discount based on payment mode. Also
display net payable amount
Mode Discount
Credit Card 10% of bill amount
Debit Card 5% of bill amount
Net Banking 2% of bill amount
otherwise 0
5. WAP to enter two number and ask the operator (+ - * / )
from the user and display the result by applying operator on
the two number
6. WAP to enter any character and print it is Upper Case,
Lower Case, Digit or symbol
7. WAP to enter 3 number and print the largest number
8. WAP to input day number and print corresponding day
name for e.g if input is 1 output should be SUNDAY and so on.
Nested if
In this type of “if” we put if within another if as a
statement of it. Mostly used in a situation where we
want different else for each condition. Syntax:
if condition1:
if condition2:
statements
else:
statements
elif condition3:
statements
else:
statements
WAP to check entered year is leap year or not