005 - 18-05-2020 -XI- Python Prog#3(Conditional statement)
005 - 18-05-2020 -XI- Python Prog#3(Conditional statement)
XI
Python Programming CS
- III
Conditional Statement
II
Content Preview I
Conditional statements:
● Syntax and examples using if
● Syntax examples using if-else
● Syntax examples using if-elif-else
● Simple programs using Conditional Statements
2
II
Conditional Statement using if I
Syntax: Example:
if <Condition>: if A>999:
<Statement> print("More")
3
II
Conditional Statement using if I
Here is an example. 4
II
Conditional Statement using if I
OUTPUT1 OUTPUT2
TYPE YOUR ANSWER IN CAPS TYPE YOUR ANSWER IN CAPS
Largest State of India:RAJASTHAN Largest State of India:MP
Correct
7
II
Conditional Statement using if I
Age=int(input("Age:"))
if Age>=18:
print("Eligible to VOTE")
print("Can get License to Drive")
OUTPUT1 OUTPUT2
Age:21 Age:11
Eligible to VOTE
Can get License to Drive
8
II
Conditional Statement using if..else I
Syntax: Example:
if <Condition>: if A>999:
<Statement1> print("More")
else: else:
<Statement2> print("Not More")
9
II
Conditional Statement using I
if..elif..else
Syntax: Example:
if <Condition1>: if A>999:
<Statement1> print("More")
elif <Condition2>: elif A>499:
<Statement2> print("OK")
... else:
else: print("Not More")
<StatementN>
10
II
Conditional Statement using if..else I
OUTPUT1 OUTPUT2
TYPE YOUR ANSWER IN CAPS TYPE YOUR ANSWER IN CAPS
Largest State of India:RAJASTHAN Largest State of India:MP
Correct It is RAJASTHAN
11
II
Problem 2 I
Write a Python code to accept Age from the users, and display a
message as Eligible to VOTE and Can get License to Drive if the Age is
greater or equal to 18, else it should convey how many years to wait
for the same:
Sample User input for Message
Age
Age: 25 Eligible to VOTE
Can get License to Drive
Age: 11 Wait for 7 years
to Vote and for Driving License
12
II
Conditional Statement using if..else I
Age=int(input("Age:"))
if Age>=18:
print("Eligible to VOTE")
print("Can get License to Drive")
else:
print("Wait for",18-Age,"years")
print("to Vote and for getting Driving License")
OUTPUT2
OUTPUT1 Age:25
Eligible to VOTE
Age:14 Can get License to Drive
Wait for 4 years
to Vote and for Driving License
13
II
Problem 3 I
Write a Python code to accept Name and Gender from the users, and
display a personalised message “Good Day Mr. Raj Singh” or “Good
Day Ms. Jia Khan” as per the Gender:
Sample User input for Name and Message
Gender
Name: Raj Singh Good Day Mr. Raj Singh
Gender: M
Name: Jia Khan Good Day Ms. Jia Khan
Gender: F
Name=input("Name:")
Gender=input("Gender(M/F):")
if Gender=="M":
print("Good Day Mr."+Name)
else:
print("Good Day Ms."+Name)
OUTPUT1 OUTPUT2
Name:Rajshree Name:Tarundeep
Gender(M/F):F Gender(M/F):M
Good Day Ms.Rajshree Good Day Mr.Tarundeep
15
II
Problem 4 I
Write a Python code to accept Color code as R for Red and G for Green
from the users, and find out if the Vehicle is allowed to Go or Stop:
16
II
Conditional Statement using if..else I
OUTPUT1
Light=input("R:Red G:Green =>")
if Light=="R": R:Red G:Green =>R
Stop
print("Stop")
***
if Light=="G":
print("Go") OUTPUT2
else:
R:Red G:Green =>G
print("***") Go
OUTPUT1
Light=input("R:Red G:Green =>")
if Light=="R": R:Red G:Green =>R
Stop
print("Stop")
elif Light=="G":
print("Go") OUTPUT2
else:
R:Red G:Green =>G
print("***") Go
Do you think
the issue
18
fixed...
II
Problem 5 I
Write a Python code to accept Age and Gender from the users, and
find out if the person’s age is legal age for marriage or not as per the
following criteria as led down by the government:
Age & Gender Message
Age equal to or more than 21 Legal age for marriage
and Gender is “M” (M:Male)
Age equal to or more than 18 Legal age for marriage
and Gender is “F” (F:Female)
Any other condition Not Eligible
19
II
Conditional Statement using if..else I
OUTPUT1
Gender=input("Gender (M/F)?")
Age=int(input("Age:")) Gender (M/F)?M
Age:34
if Gender=="M" and Age>=21:
Legal age for marriage
print("Legal age for marriage")
elif Gender=="F" and Age>=18: OUTPUT2
print("Legal age for marriage") Gender (M/F)?F
else: Age:20
Legal age for marriage
print("Not Eligible")
OUTPUT1
Gender=input("Gender (M/F)?")
Gender (M/F)?M
Age=int(input("Age:"))
Age:34
if (Gender=="M" and Age>=21) \ Legal age for marriage
or (Gender=="F" and Age>=18):
print("Legal age for marriage")
else: OUTPUT2
print("Not Eligible")
Gender (M/F)?F
Age:20
Legal age for marriage
22
II
Problem 6 I
Write a Python code to accept Price and Qty of item from the users,
assign discount percentage as per the following criteria and calculate
the discounted amount for the customer:
Amount Discount %
More than 500 20
Less or Equal to 500 10
23
II
Conditional Statement using if..else I
Price=float(input("Price:")) OUTPUT1
Qty=int(input("Qty:")) Price:120
Amt=Price*Qty Qty:5
print("Amt:",Amt) Amt: 600.0
if Amt>500: Discounted Amt: 480.0
Discount=20
else: OUTPUT2
Discount=10 Price:80
Amt=Amt-Amt*Discount/100 Qty:4
print("Discounted Amt:",Amt) Amt: 320.0
Discounted Amt: 288.0
24
II
Problem 7 I
Basic=float(input("Basic:")) OUTPUT1
if Basic>100000: Basic:120000
ITax=0.2*Basic ITax: 24000.0
All=0.4*Basic Allowance: 48000.0
else: In hand: 144000.0
ITax=0.1*Basic
All=0.3*Basic OUTPUT2
InHand=Basic-ITax+All Basic:50000
print("ITax:",ITax) ITax: 5000.0
print("Allowance:",All) Allowance: 15000.0
print("In hand:",InHand) In hand: 60000.0
26
II
Problem 8 I
Write a Python code to accept Vehicle Type and Speed of vehicle from
user, decide and display, it comes under overspeed challan or not as
per the following criteria:
Vehicle Type and Speed Limit Challan Status
Vehicle Type: Bus or Truck If above the Speed Limit
Speed Limit: 60 under the Vehicle Type, it will
be “Challaned”
Vehicle Type: Car
Speed Limit: 80
Any other category or with in “No Challan”
Speed Limit
27
II
Conditional Statement using if..else I
28
II
Problem 9 I
30
II
Problem 10 I
31
II
Conditional Statement using I
if..elif..else
Shape=input("S:Square \ print("Area:",A)
R:Rectangle C:Circle ") print("Perimeter:",P)
if Shape=="S":
S=int(input("Side:")) OUTPUT1
A=S*S;P=4*S S:Square R:Rectangle C:Circle R
elif Shape=="R": Length:20
L=int(input("Length:")) Breadth:7
B=int(input("Breadth:")) Area: 140
A=L*B;P=2*(L+B) Perimeter: 54
elif Shape=="C": OUTPUT2
R=float(input("Radius:"))
A=(22/7)*R*R;P=2*(22/7)*R S:Square R:Rectangle C:Circle C
else: Radius:7
print("Unknown Shape") Area: 154.0
A=0;P=0 Perimeter: 44.0 32
II
Problem 11 I
Write a Python code to accept name of the month and display number
of days in that month as follows (in case of FEB, take input for Year also:
Month Name Days Month Name Days
JAN 31 JUL 31
FEB 28 or 29 AUG 31
MAR 31 SEP 30
APR 30 OCT 31
MAY 31 NOV 30
JUN 30 DEC 31
33
II
Conditional Statement using I
if..elif..else
print("TYPE IN CAPS") elif M=="APR": elif M=="NOV":
M=input("Month 3 letters:") D=30 D=30
if M=="JAN": elif M=="MAY": elif M=="DEC":
D=31 D=31 D=31
elif M=="FEB": elif M=="JUN": else:
Y=int(input("Year:")) D=30 print("WRONG")
if (Y%4==0 and Y%100!=0)\ elif M=="JUL": D=0
or Y%400==0: D=31 print("DAYS",D)
D=29 elif M=="AUG":
else: D=31 OUTPUT
D=28 elif M=="SEP":
elif M=="MAR": D=30 TYPE IN CAPS
D=31 elif M=="OCT": Mon 3 letters:FEB
D=31 Year:2100
DAYS 28
34
II
Conditional Statement using I
if..elif..else
print("TYPE IN CAPS") elif M=="APR" or M=="JUN" \
M=input("Mon 3 letters:") or M=="SEP" or M=="NOV":
if M=="JAN" or M=="MAR" or \ D=30
M=="MAY" or M=="JUL" or \ else:
M=="AUG" or M=="OCT" or \ print("WRONG MONTH")
M=="DEC": D=0
D=31 print("DAYS",D)
elif M=="FEB":
Y=int(input("Year:"))
if (Y%4==0 and Y%100!=0) \ OUTPUT2
or Y%400==0: TYPE IN CAPS
D=29 Mon 3 letters:FEB
else: Year:2100
D=28 DAYS 28
35
II
Problem 12 I
Write a Python code to accept three numbers and find minimum and
maximum value out of three.
38
II
Conditional Statement using if.. I
A=int(input("A:")) OUTPUT1
B=int(input("B:"))
A:45
C=int(input("C:")) B:93
C:74
if B < A: 45 74 93
A,B = B,A
OUTPUT2
if C < B:
B,C=C,B A:45
if B < A: B:23
B,A=A,B C:34
23 34 45
print(A,B,C)
39
II
Conditional Statement using if.. I
A=int(input("A:")) OUTPUT1
B=int(input("B:"))
A:45
C=int(input("C:")) B:93
C:74
if B > A: 93 74 45
A,B = B,A
OUTPUT2
if C > B:
B,C=C,B A:45
if B > A: B:23
B,A=A,B C:34
45 34 23
print(A,B,C)
40
II
Conditional Statement using if.. I
A=int(input("A:")) Arranging 4 OUTPUT1
B=int(input("B:")) distinct
C=int(input("C:")) A:45
values in B:93
D=int(input("D:"))
order C:74
if B < A:
A,B = B,A D:32
32 45 74 93
if C < B:
if D < C: OUTPUT2
B,C=C,B
C,D=D,C
if B < A: A:45
if C < B:
B,A=A,B B:12
B,C=C,B
C:34
if B < A:
D:23
A,B=B,A
12 23 34 45
print(A,B,C,D)
41
II
Problem 14 I
Write a Python code to accept a number and check if it is divisible by 2,
or/and divisible by 3, or/and divisible by 5, or/and divisible by 7 or not.
42
II
Conditional Statement using if.. I
N=int(input("N:")) OUTPUT1
if N%2==0: N:40
40 is divisible by 2
print(N,"is divisible by 2")
40 is divisible by 5
if N%3==0:
print(N,"is divisible by 3")
if N%5==0: OUTPUT2
print(N,"is divisible by 5") N:21
if N%7==0: 21 is divisible by 3
21 is divisible by 7
print(N,"is divisible by 7")
43
II
Problem 15 I
Write a Python code to accept values of A,B and C of a Quadratic
equation and find its roots.
As you already know that first you need to calculate value of D, as
follows:
D=B*B-4*A*C.
If the value of D>0, there will real and unequal roots, which are as
follows:
R1=(-B+D**0.5)/2*A
R1=(-B-D**0.5)/2*A
If the value of D==0, there will real and equal roots, which is as follows:
R=-B/2*A
44
II
Conditional Statement using if.. I
A=float(input("A:")) OUTPUT1
B=float(input("B:"))
C=float(input("C:")) A:2
D=B*B-4*A*C B:-8
if D>0: C:3
R1=(-B+(D**0.5))/2*A Real and Unequal roots
R2=(-B-(D**0.5))/2*A 14.32 1.67
print("Real and Unequal
OUTPUT2
roots",R1,R2)
elif D==0: A:1
R=-B/2*A B:-18
print("Real and Equal C:81
roots",R) Real and Equal roots 9.0
else:
print("No real roots") 45
II
I
Thank You!
46