5-Python - IF
5-Python - IF
1. If statement
2. If - Else Statement
3. If - Elif – Else Statement
4. Nestled If
5. Few Programming Examples
Python – IF Statements
if e x p r e s s i o n : if expression1:
statement(s) statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
if expression: ●
statement(s) ●
else: ●
statement(s) else:
statement(s)
Examples:
var1 = 100
if var1:
print("1 - Got a True expression value“)
print(var1)
var2 = 0
if var2:
print("2 - Got a True expression value“)
print(var2)
var1 = 100
if var1:
print ("1 - Got a True expression value“)
print (var1)
else:
print ("1 - Got a false expression value“)
print (var1)
var2 = 0
if var2:
print ("2 - Got a True expression value“)
print (var2)
else:
print ("2 - Got a false expression value“)
print (var2)
A company is decided to give rating to employees out of 5 stars based
on his/her performance. So they gave a score from 1 to 200 to each
employee. rating will be given for their score as shown bellow.
151 to 200 *****
101 to 150 ****
51 to 100 ***
1 to 50 **
X = int( input(Enter Employee Score:))
if X <= 200 and X > 0:
if X > 150:
print(“ ***** “)
elif X > 100:
print(" **** “)
elif X > 50:
print(" *** “)
else:
print(“ ** “ )
else:
print(“ Rating should be in range 1 to 200“)
Single Statement Suites:
If the suite of an if clause consists only of a single line, it may go on the same line as the header statement:
Output:
2
Welcome
The Entry tickets for the show is to be printed with a Welcome message along with an
additional message for Children stating they should be accompanied by an adult. Given
the age of the person visiting the scary house, the ticket should carry the additional
message only for Children whose age is less than 15 years. The show organizers wanted
your help to accomplish this task. Write a program that will get age as the input and
display the appropriate message on the tickets.
Sample Input 1:
20
Sample Output 1:
3
Welcome to the show
Sample Input 2:
14
Sample Output 2:
Welcome to the show
Please note that you should be accompanied by an adult
Lucky Winner
The visitors whose ticket number has the last digit as 3 or 8, are declared as 4
lucky winners and attracting prizes are awaiting to be presented for them.
Write a program to find if the last digit of the ticket number of visitors is 3 or 8.
Sample Input 1:
43
Sample Output 1:
Lucky Winner
Sample Input 2:
41
Sample Output 2:
Not a Lucky Winner
Card Game
The game’s rules are: 5
A player needs to pick 3 cards from a big lot of cards. There are 4 types of Cards namely
Spade(S), Heart(H), Club(C) and Diamond (D). If all the 3 cards that the player picks are of
the same type and same number, they get a Double Bonanza. If all the 3 cards are of the
same type or if they all have the same number, they get a Bonanza. Otherwise they do not
get a Bonanza. Alan has now picked 3 cards and is awaiting to know if he has got a
bonanza. Please help him to know if he has won the Bonanza or not.
If the angles selected by a kid forms a triangle, he/she would receive Prize 1. If the angles
forms a right triangle, he/she would receive Prize 2. If the angles form an equilateral
triangle, he/she would receive Prize 3. If the angles do not form even a triangle, then
he/she will not receive any prizes. Write a program for the organizers to fetch the result
based on the number boards selected by the kids.
Sample Input 2:
Sample Input 1: 60
60 60
50 70
70 Sample Output 2:
Sample Output 1: No Prize
Prize 1
E or e - Early Bird Ticket
D or d - Discount Ticket
V or v - VIP Ticket
7
S or s - Standard Ticket
C or c - Child Ticket
Write a piece of code for the scanning machine that will take the input of a character and
print the equivalent string as given.
Sample Input 1:
e
Sample Output 1:
Early Bird Ticket
Sample Input 2:
S
Sample Output 2:
Standard Ticket
Total Expenses
Write a program to help the Organizers to create the portal as per the requirement
given below.
8
Given the ticket cost as 'X‘, Number of tickets ‘N’.
If the number of tickets purchased is
less than 50, there is no discount.
between 50 and 100 , then 10% discount is offered.
between 101 and 200, 20% discount is offered.
between 201 and 400, 30% discount is offered.
between 401 and 500, 40% discount is offered.
greater than 500, then 50% discount is offered.
Display the amount need to be paid.
Sample Input 1:
12000
Sample Output 1:
24600.00
Sample Input 2:
30000
Sample Output 2:
64400.00
Grades of Rides
“AquaticaCarnival” is the most successful event dedicated to children and families. 10
The Event has more than 20 rides for children and adults and the organizers always ensure not to
compromise on the safety of the visitors.
To ensure the safety of the rides, the organizers have graded the rides in the fair according to the
following conditions:
(i)Hurl Factor must be greater than 50. Sample Input 1:
(ii)Spin Factor must be greater than 60. 51 89 150
(iii)Speed factor must be greater than 100.
Sample Output 1:
10
The grades are as follows:
Grade is 10 if all three conditions are met. Sample Input 2:
Grade is 9 if conditions (i) and (ii) are met. 45 69 102
Grade is 8 if conditions (ii) and (iii) are met.
Grade is 7 if conditions (i) and (iii) are met. Sample Output 2:
Grade is 6 if only one condition is met. 8
Grade is 5 if none of three conditions are met.
Write a program display the grade of the rides, given the values of hurl factor, spin factor and
speed factor of the ride under consideration.