Simple IF structure
Q1. Input a number and print if it is even or odd.
print("Enter a number")
num = int(input())
if num % 2 == 0:
print("Even")
else:
print("Odd")
2. Input a number and print if it is even or odd. Input the percentage of a student in Examination and
print if the student has passed or failed.
print("Enter percentage: ")
percentage = float(input())
if percentage >= 50:
print("Pass")
else:
print("Fail")
3. A passenger is allowed to carry 30kg of luggage with him. If there is extra luggage, then he has to
pay fine at the rate Rs.200 per kg. Write a program to input the weight of luggage and print the Fine if
there is any or Zero otherwise.
print("Enter luggage weight: ")
weight = float(input())
extra = weight - 30
fine = extra * 200
if extra > 0:
print("Total fine is",fine)
else:
print("fine is",0)
4. An employee is paid salary depending on the number of hours worked and his hourly rate. If he
works more than 40 hours in a week than he is paid over time and rate for overtime is double the
normal rate. Write a program to input the hourly rate and hours worked of an employee. Print this
Normal Salary, Overtime Salary and Gross Salary.
print("Hourly rate: ")
hourly_rate = int(input())
print("Hours worked:")
hours = int(input())
normal = 40 * hourly_rate
extra = hours – 40
overtime = extra * hourly_rate * 2
print("Normal:",normal)
print("Overtime:",overtime)
print("Gross:", normal + overtime)
print("Extra hours:", extra)
IF …. ELSEIF structure
5. Input the percentage of a student in an examination and print his grade.
>= 90 A
>= 80 B
>=70 C
>=60 D
>=50 E
print("Enter percentage: ")
percentage = float(input())
if percentage >= 90:
grade = "A"
else:
if percentage >= 80:
grade = "B"
else:
if percentage >= 70:
grade = "C"
else:
if percentage >= 60:
grade = "D"
else:
if percentage >= 50:
grade = "E"
else:
grade = "U"
print(grade)
6. Input the Cholesterol Level in a person. Print according to the range
>= 240 High Risk
> 200 Risky
<= 200 Normal
print("Cholesterol level: ")
level = int(input())
if level >= 240:
print("High Risk")
else:
if level > 200:
print("Risky")
else:
if level <= 200:
print("Normal")
IF within IF structure
7. There are two types of ticket issued by an airline. Business and Economy. Both are allowed to carry a
limited luggage weight them. Write a program to input the ticket type and weight of luggage of the
passenger. Calculate his fine according to the grid given below or Zero otherwise.
Business Allowed weight 50kg Rs. 200/kg Fine for extra weight
Economy Allowed weight 30kg Rs. 400/kg Fine for extra weight
print("Ticket type Business/Economy: ")
ticket_type = str(input())
print("Luggage weight: ")
weight = int(input())
if ticket_type == "business":
weightallowed = 50
rate = 200
extra = weight - weightallowed
if extra > 0:
fine = extra * rate
print("Please pay fine:",fine)
else:
print("fine is: ", 0)
if ticket_type == "Economy":
weightallowed = 30
rate = 400
extra = weight - weightallowed
if extra > 0:
fine = extra * rate
print("Please pay fine:",fine)
else:
print("fine is: ", 0)
8. A student takes two tests in a semester. Max Marks are 100. His grade is “Distinction” if he scores
80 or above in both the subjects “Pass” if Aggregate is 100 or above “Fail” otherwise
print("Test 1 marks: ")
test1 = float(input())
print("Test 2 marks: ")
test2 = float(input())
if test1 >= 80 and test2 >= 80:
print("Distinction")
else:
if test1 + test2 >= 100:
print("Pass")
else:
print("Fail")
9. Input three numbers and print the Largest.
print("First number: ")
a = int(input())
print("Second number: ")
b = int(input())
print("Third number: ")
c = int(input())
if a > b and a > c:
print ("largest number is a",a)
else:
if b > a and b > c:
print ("largest number is b",b)
else:
if c > a and c > b:
print ("largest number is c",c)
Logical Operators AND, OR, NOT
10. Input the marks obtained by a student in test. Print if the Marks are Valid or invalid. Marks from 0
to 100 (inclusive) means valid.
print("Enter marks: ")
marks = int(input())
if marks >= 0 and marks <= 100:
print("marks is Valid")
else:
print ("marks is Invalid")
11. There are two types of ticket issued by an airline. Business and Economy. Both are allowed to carry a
limited luggage weight them. Write a program to input the ticket type and weight of luggage of the
passenger. Calculate his fine according to the grid given below or Zero otherwise.
Business Allowed weight 50kg Rs. 200/kg Fine for extra weight
Economy Allowed weight 30kg Rs. 400/kg Fine for extra weight
print("Ticket type Business/Economy: ")
ticket_type = str(input())
print("Luggage weight: ")
weight = int(input())
if ticket_type == "business" and weight > 50:
weightallowed = 50
rate = 200
extra = weight - weightallowed
if extra > 0:
fine = extra * rate
print("Please pay fine:",fine)
if ticket_type == "business" and weight <= 50:
print("fine is: ", 0)
if ticket_type == "Economy" and weight > 30:
weightallowed = 30
rate = 400
extra = weight - weightallowed
if extra > 0:
fine = extra * rate
print("Please pay fine:",fine)
if ticket_type == "Economy" and weight <= 30:
print("fine is: ", 0)