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

005 - 18-05-2020 -XI- Python Prog#3(Conditional statement)

The document provides an overview of conditional statements in Python, including syntax and examples for 'if', 'if-else', and 'if-elif-else' structures. It includes practical programming problems that demonstrate how to implement these statements for various scenarios, such as age eligibility, traffic light responses, and scholarship calculations. Additionally, it covers the use of relational and logical operators in conjunction with conditional statements.

Uploaded by

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

005 - 18-05-2020 -XI- Python Prog#3(Conditional statement)

The document provides an overview of conditional statements in Python, including syntax and examples for 'if', 'if-else', and 'if-elif-else' structures. It includes practical programming problems that demonstrate how to implement these statements for various scenarios, such as age eligibility, traffic light responses, and scholarship calculations. Additionally, it covers the use of relational and logical operators in conjunction with conditional statements.

Uploaded by

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

II

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

A new code block (body of an


if, etc.) starts with indentation
and ends with the first if A>999 :
unindented line. The amount of
indentation is up to you, but it print("More",end=" ")
must be consistent throughout
that block. print("than")
In our examples, we will be print("required")
using two white spaces for
indentation (preferred over
print("OVER")
tabs for indentation).

Here is an example. 4
II
Conditional Statement using if I

Minimum number of white


spaces required in indentation:
1 if A>999 :
Sufficient number of white print("More",end=" ")
spaces required in indentation:
2
print("than")
(But it should be uniform within
one block of code)
print("required")
If the value of A is more
From thanof
if block
999 code
print("OVER")
This code From
will print
outside block of
More thancode 5
II
Relational and Logical Operators used I
with if Relational Operators
Boolean/Logical Operators

Order Operator True when Operato Description


r
1 not Operand is
False == Equal to

2 and Both Operands > Greater than


are True
< Less than
3 or Any one
>= Greater than or Equal
operand is True
to
<= Less than or Equal to
!= No equal to 6
II
Conditional Statement using if I

print("TYPE YOUR ANSWER IN CAPS")


S=input("Largest State of India:")
if S=="RAJASTHAN":
print("Correct")

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

print("TYPE YOUR ANSWER IN CAPS")


S=input("Largest State of India:")
if S=="RAJASTHAN":
print("Correct")
else:
print("It is RAJASTHAN")

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

If Gender input is anything other than "M", it should display the


output considering Gender as "F"
14
II
Conditional Statement using if..else I

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:

Color of Traffic Light Message


R STOP
G GO
Any other input ***

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

Do you see any


issue in LOGIC of
17
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")
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

Gender=input("Gender (M/F)?") OUTPUT1


Age=int(input("Age:"))
if Gender=="M" and Age>=21: Gender (M/F)?M
print("Legal age for marriage") Age:34
Legal age for marriage
if Gender=="F" and Age>=18:
Not Eligible
print("Legal age for marriage")
else:
OUTPUT2
print("Not Eligible")
Gender (M/F)?F
Age:20
Do you see any Legal age for marriage
issue in LOGIC of
the code 20
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")

Do you think the


issue fixed... Can we enhance it further
21
II
Conditional Statement using if..else I

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

Note: Amount to be calculated as Price*Qty

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

Write a Python code to accept Basic Salary of an employee, calculate


Income Tax, Allowances and Salary In hand for the employee as per
the following criteria:
Basic Salary Income Tax and Allowance
More than 100000 ITax = 20% of Basic Salary
Allowance=40% of Basic
Salary
Less or Equal to 100000 ITax = 10% of Basic Salary
Allowance=30% of Basic
Salary
Note: For calculation of Inhand Salary, subtract the Income Tax
Amount from Basic Salary and add the Allowance in Basic Salary
25
II
Conditional Statement using if..else 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

VType=input("C:Car T:Truck B:Bus:") OUTPUT1


Speed=int(input("Speed:")) C:Car T:Truck B:Bus:C
if (VType=="C" and Speed>=80) \ Speed:87
or ((VType=="T" or VType=="B")\ OverSpeed Challan
and Speed>=60):
print("Over Speed Challan")
else: OUTPUT2
print("Not Overspeeding OR") C:Car T:Truck B:Bus:Car
print("VType is incorrect") Speed:70
Not Overspeeding OR
VType is incorrect

28
II
Problem 9 I

Write a Python code to accept marks of three subjects, calculate


average marks of three subjects and decide for the amount of
scholarship as per the following criteria
Average Marks Criteria Scholarship Amount
If the student gets Average For every 1% above 90,
Marks more than 90 or equal he/she should Rs. 3000 and
to 90 for exact 90, it should be Rs.
3000
If the student gets Average For every 1% above 75,
Marks more than or equal to he/she should Rs. 200 and for
75 exact 75, it should be Rs. 200
only 29
II
Conditional Statement using I
if..elif..else
Eng=int(input("English:")) OUTPUT1
Mat=int(input("Maths:"))
English:89
Sci=int(input("Science:"))
Maths:89
Avg=(Eng+Mat+Sci)/3
Science:89
if Avg>=90:
Scholarship Amt: 2800.0
Amt=(Avg-89)*3000
print("Scholarship Amt:",Amt)
elif Avg>=75: OUTPUT2
Amt=(Avg-74)*200 English:97
print("Scholarship Amt:",Amt) Maths:96
else: Science:98
print("Re-appear for the Test") Scholarship Amt: 24000.0

30
II
Problem 10 I

Write a Python code to accept shape and accept Length,Breadth or Side


or Radius depending on shape, calculate perimeter and area of shape
accordingly

Shape Area and Perimeter


S:Square A=Side*Side;P=4*Side

R:Rectangle A=L*B; P=2*(L+B)

C:Circle A=pi*R*R; P=2*pi*R

Any other A=0;P=0

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.

Sample input and output of the code are as follows::


Input Minimum and Input Minimum and
Maximum Values Maximum
Values
A:34 Minimum:23 A:59 Minimum:14
B:45 Maximum:45 B:14 Maximum:59
C:23 C:35
A:18 Minimum:18 A:94 Minimum:12
B:41 Maximum:78 B:45 Maximum:94
C:78 C:12 36
II
Conditional Statement using if.. I
A=int(input("A:")) OUTPUT1
B=int(input("B:"))
C=int(input("C:")) A:45
Min,Max=A,A B:93
if Min>B: C:74
Min=B Minimum: 45
if Min>C: Maximum: 93
Min=C
OUTPUT2
if Max<B:
Max=B A:45
if Max<C: B:23
Max=C C:34
print("Minimum:",Min) Minimum: 23
print("Maximum:",Max) Maximum: 45
37
II
Problem 13 I
Write a Python code to accept three numbers and arrange them in
ascending order.

Sample sets of inputs and outputs of the code are as follows::


Input Ascending order Input Ascending Order
A:34 23 34 45 A:59 14 35 59
B:45 B:14
C:23 C:35
A:78 18 41 78 A:94 12 45 94
B:41 B:45
C:18 C:12

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.

Sample sets of inputs and outputs of the code are as follows::


Input Ascending order Input Ascending Order
N:21 21 is divisible by 3 N:36 36 is divisible by 2
21 is divisible by 7 36 is divisible by 3

N:26 26 is divisible by 2 N:40 40 is divisible by 2


40 is divisible by 5

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!

COMPUTER SCIENCE DEPARTMENT


DELHI PUBLIC SCHOOL R K PURAM

46

You might also like