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

5-Python - IF

The document provides an overview of if statements in Python including if-else, if-elif-else, nested if statements, and examples. It discusses evaluating boolean expressions and executing code blocks based on expression results. Several examples are given to demonstrate if statements for rating employee performance, finding lucky ticket numbers, determining card game and triangle game prizes, and computing ticket, salary, and carnival ride grades based on conditional logic.

Uploaded by

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

5-Python - IF

The document provides an overview of if statements in Python including if-else, if-elif-else, nested if statements, and examples. It discusses evaluating boolean expressions and executing code blocks based on expression results. Several examples are given to demonstrate if statements for rating employee performance, finding lucky ticket numbers, determining card game and triangle game prizes, and computing ticket, salary, and carnival ride grades based on conditional logic.

Uploaded by

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

Class Overview

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:

if ( expression == 1 ) : print ("Value of expression is 1“)


Input a positive integer number ‘N’. Convert the given decimal number
to binary. Complement all the bits. And display the results in decimal. 1
Test Case1:
Input: 10
Output: 5

[Binary representation of 10 is 1010. After complimenting the bits(1010), will get


0101 which represents “5”. Hence output will print “5”.]

Test Case2: Program (Method1): Program (Method2):


Input: 5 import math import math
Output: 2 n=int(input()) n=int(input())
k= int(math.log2(n)) k=(1<<int(math.log2(n))+1)-1
Test Case3: k=(2**(k+1))-1 print(n^k)
Input: 18
print(n^k)
Output: 13
Count Number of occurrences
Input a String
Count, how many times Letter ‘n’ exist in that.
2
Input:
assignment

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.

Sample Input 1: Sample Input 2:


S5 S6
S5 S5
S5 H5
Sample Output 1:
Double Bonanza Sample Output 2:
No Bonanza
Triangle Game
Here different number boards in the range 1 to 180 are available. Each kid needs 6
to select three number boards, where the numbers on the boards correspond to the
angles of a triangle.

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: Sample Input 2:


100 100
5 300
Sample Output 1: Sample Output 2:
500.00 21000.00
Salary Computation
If his basic salary is less than Rs. 15000, then HRA = 15% of basic salary and 9
DA = 90% of basic salary.
If his basic salary is either equal to or above Rs. 15000, then HRA = Rs. 5000 and DA = 98%
of basic salary.
If the Danny’s salary is given as input, write a program to find his gross salary.
Note : Gross Salary = Basic Salary + HRA + DA

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.

You might also like