PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • Questions Using If - else block
  • Questions using if-else ladder and nested if-else
  1. Python For Data Analytics
  2. 1.Python
  3. 2.Python Assignments

Conditional Statements Assignments

Expected time-- 3 hrs.

Questions Using If - else block

  1. In a classic chase, Tom is running after Jerry as Jerry has eaten Tom's favourite food.

Jerry is running at a speed of XXX metres per second while Tom is chasing him at a speed of YYY metres per second. Determine whether Tom will be able to catch Jerry.

Note that initially Jerry is not at the same position as Tom. Take X of Jerry and Y of Tom from the user and make decision and print YES if tom can catch and NO if not. Sample Input

2 3
4 1
1 1
3 5

Sample Output

YES
NO
NO
YES
Solution
x = int(input('Enter Jerry Speed : '))
y = int(input('Enter Tom Speed : '))

if y > x:
    print('YES')
else:
    print('NO')
  1. Apple considers any iPhone with a battery health of 8080%80% or above, to be in optimal condition.

Given that your iPhone has XX%X%battery health, find whether it is in optimal condition.

Take Battery Health from User and print YES if in optimal condition and NO if not. Sample Input

97
42
80
10

Sample Output

YES
NO
YES
NO
Solution
x = int(input('Enter Battery health : '))
if x >= 80:
    print('YES')
else:
    print('NO')

  1. While purchasing certain items, a discount of 10 % is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.

Solution
qty = int(input(‘Enter the value of quantity : ’))
price  = int(input(‘Enter the Price : ’))
dis = 0
if qty > 1000:
	dis=  10
else:
	dis = 0

Totexp = qty*price-qty*price*dis/100
print(f‘Total Expense : Rs {Totexp}’)
  1. A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.

Solution
salary = float(input("Enter salary : "))
yos = int(input("Enter year of service : "))
if yos>5:
  print("Bonus is",(5/100)*salary)
else:
  print("No bonus")
  1. A University allows students to sit in the exam despite of insufficient attendance if the student's attendance is short due to some medical cause. Take Input from User whether he had medical cause (Y/N) and print the message you are allowed if medical cause == Y otherwise print you are not allowed.

Solution
medical_cause = input('Input Whether you had any medical cause or not (Y/N)')
if medical_cause == 'Y':
    print('You are allowed')
else:
    print('You are not allowed')
  1. Write a program to check whether the last digit of the number is divisible by 3 or not. Take Number from user and your output must contain the number.

Solution
number = int(input('Enter Number : '))
if (number%10)%3 == 0:
    print(f'{number} is divisble by 3')
else:
    print(f'{number} is not divisible by 3')
  1. Write a program to display ‘Hello’ if the number entered by user is multiple of 5 otherwise prints ‘Bye’.

Solution
num = int(input('Enter the number : '))
if num % 5 == 0:
    print('Hello')
else:
    print('Bye')
  1. Write a program to check whether a person is eligible for voting or not.(voting age >=18)

Solution
age = int(input('Enter age : '))
if age >= 18:
    print('Eligible')
else:
    print('Not Eligible')
  1. Write a program to check whether a number entered by user is even or odd.

Solution
num = int(input("Enter the number : "))
if num %2 == 0:
    print(f'{num} is even')
else:
    print(f'{num} is odd')
  1. Write a program to check whether a given character is a vowel or a consonant.

Solution
char = input("Enter a character: ")
if char in "AEIOUaeiou":
    print(char, "is a vowel")
else:
    print(char, "is a consonant")
  1. Write a program to check whether a given string is a valid email address (i.e., it contains exactly one "@" symbol and at least one "." after the "@").

Solution
email = input("Enter an email address: ")
if email.count("@") == 1 and "." in email[email.index("@"):]:
    print("The email address is valid")
else:
    print("The email address is not valid")

Questions using if-else ladder and nested if-else

  1. Write a program to calculate the electricity bill (accept number of unit from user) according to the following criteria :

Unit
Price

First 100 Units

No charge

Next 100 Units

5 Rs Per Unit

After 200 Units

10 Rs Per Unit

Solution

no_of_unit = int(input('Units : '))

if(no_of_unit<=100):

bill = 0

elif(no_of_unit>100 and no_of_unit<=200):

bill = (no_of_unit-100) * 5

else:

bill = 500 + (no_of_unit - 200)*10

print(bill)

  1. Write a program to accept percentage from the user and display the grade according to the following criteria:

Mark
Grade

>=90

A

>=80 and <90

B

>=60 and <80

C

<60

D

Enter the marks : 87

B

Solution

marks = int(input('Enter the marks : '))

if(marks>90):

print('A')

elif(marks>80 and marks<=90):

print('B')

elif(marks>60 and marks<=80):

print('C')

else:

print('D')

  1. Write a python program that will check for the following conditions:

· If the light is green – Car is allowed to go

· If the light is yellow – Car has to wait

· If the light is red – Car has to stop

· Other signal – unrecognized signal. Example black, blue, etc…

Solution
light = input('Enter Traffic Light : ').lower()
if light == 'green':
    print('Car is allowed to go')
elif light == 'yellow':
    print('Car has to wait')
elif light == 'red':
    print('Car has to stop')
else:
    print(f'unrecognized signal {light}')
  1. Write a program to check whether the given number is positive, zero or negative.

Solution
num = int(input('Enter number : '))
if num <0:
    print('Negative')
elif num == 0:
    print('Zero')
else:
    print('Positive')
  1. Calculate income tax for the given income by adhering to the below rules

Taxable Income Rate (in %)

First Rs.10,0000 0

Next Rs. 10,0000 10

The remaining 20

  1. Write a program to take a digit and print it is In words(only 0 - 9) For Example : enter digit : 5 Five

Solution
digit = int(input('Enter Digit : '))
if digit >=0 and digit <= 9:
    if digit == 0:
        print('Zero')
    elif digit == 1:
        print('One')
    elif digit == 2:
        print('Two')
    elif digit == 3:
        print('Three')
    elif digit == 4:
        print('Four')
    elif digit == 5:
        print('Five')
    elif digit == 6:
        print('Six')
    elif digit == 7:
        print('Seven')
    elif digit == 8:
        print('Eight')
    elif digit == 9:
        print('Nine')
else:
    print('Digit is either less than 0 and greater than 9')

  1. Write a menu driven program for making a simple calculator.

Solution
print("Menu:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = int(input("Enter your choice (1-4): "))

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == 1:
    result = num1 + num2
    print("Result:", result)
elif choice == 2:
    result = num1 - num2
    print("Result:", result)
elif choice == 3:
    result = num1 * num2
    print("Result:", result)
elif choice == 4:
    if num2 == 0:
        print("Error: division by zero")
    else:
        result = num1 / num2
        print("Result:", result)
else:
    print("Invalid choice")

  1. An institution has decided to admit new candidates in different streams on the following criteria:

Total Marks Obtained Streams Offered

300 and above Science 200 and above but less than 300 Commerce Below 200 but not below 75 Arts Otherwise Admission is not granted, You have to appear in qualifying examination

Write a program to input total marks obtained in an examination and print the stream allotted on the basis of above criteria.

Solution
marks = int(input("Enter total marks obtained: "))

if marks >= 300:
    stream = "Science"
elif marks >= 200 and marks < 300:
    stream = "Commerce"
elif marks >= 75 and marks < 200:
    stream = "Arts"
else:
    stream = "Qualifying examination"

print("Stream allotted: ", stream)

  1. Design a Program to input two integers num1, num2 and a character(opr). The Variable opr reads only one of the four characters(+,-,/,*). Your Program should perform the operation on the basis of operator on num1, num2. In case of subtraction, subtract smaller number from bigger number and in case of division divide the greater number by smaller numbers. Print Integers and result.

  1. A Scooter /motor cycle stand charges the following rates for the parking:

Hours Rate

First 4hours RS.5.00 Every next hour upto5 hours RS.3.00 per hour Any further hour above 9 hour RS.2.00 per hour

Write a program to input the number of hours for which a two wheeler is parked. Calculate and print the parking charges to be paid by the customer.

  1. Monthly Electricity bill is calculated as –

Number of units Consumed Rate Per Unit

<=100 only meter rent Rs 200/- For next 200 units Rs. 1.00 per unit For next 200 units Rs 1.55 per unit For more than 500 units Rs 2.10 per unit

Write a program to take the consumer number, number of units consumed. Calculate bill amount. Print consumer number and total amount to be paid by the consumer. (Consumer number must be digits of length 5 and print appropriate message for incorrect input of consumer number).

Solution
consumer_number = input("Enter consumer number (5 digits): ")
if not consumer_number.isdigit() or len(consumer_number) != 5:
    print("Invalid consumer number!")
else:
    units_consumed = int(input("Enter number of units consumed: "))
    if units_consumed <= 100:
        total_amount = 200
    elif units_consumed <= 300:
        total_amount = 200 + (units_consumed - 100) * 1.00
    elif units_consumed <= 500:
        total_amount = 200 + 200 * 1.00 + (units_consumed - 300) * 1.55
    else:
        total_amount = 200 + 200 * 1.00 + 200 * 1.55 + (units_consumed - 500) * 2.10
    print("Consumer number:", consumer_number)
    print("Total amount to be paid:", total_amount)

  1. Write a menu driven program for three options. (Take input of option from user).

i) Area of circle ii) Area of Rectangle iii) Area of Square

Print the area along with sides.

  1. A Bank offers the following rate of interest for fixed deposit:

Time(Years) Rate(%)

<1 9.0 1 to 2 10.0 2 to 3 11.0 >3 12.0

The amount A after n years is calculated by using the formula: A = P(1 + r / 100)^n

Where P = principal amount deposited R = rate of interest N = number of years

Write a program to accept the deposited amount, number of years the amount is to be deposited for n and compute accrued amount for an investor.

Solution
p = float(input("Enter the principal amount: "))
n = float(input("Enter the number of years: "))

if n < 1:
    rate = 9.0
elif 1 <= n <= 2:
    rate = 10.0
elif 2 < n <= 3:
    rate = 11.0
else:
    rate = 12.0

amount = p * (1 + rate/100)**n

print("Principal amount: Rs.", p)
print("Rate of interest: ", r, "%")
print("Number of years: ", n)
print("Amount: Rs.", amount)
  1. Write a program to take number input between 1 to 7 and print the day associated to that number such as 1 for Sunday, 2 for Monday, etc.

Solution
day_number = int(input('Enter Day Number : '))

if day_number >= 1 and day_number <= 7:
    if day_number == 1:
        print('Sunday')
    elif day_number == 2:
        print('Monday')
    elif day_number == 3:
        print('Tuesday')
    elif day_number == 4:
        print('Wednesday')
    elif day_number == 5:
        print('Thursday')
    elif day_number == 6:
        print('Friday')
    elif day_number == 7:
        print('Saturday')
        
else:
    print('Invalid Day Number')
  1. Write a program to take number input between 1 and 12 and print the month associated with it.

Solution
month_num = int(input('Enter Month Number : '))

if month_num >= 1 and month_num <= 12:
    if month_num == 1:
        print('January')
    elif month_num == 2:
        print('February')
    elif month_num == 3:
        print('March')
    elif month_num == 4:
        print('April')
    elif month_num == 5:
        print('May')
    elif month_num == 6:
        print('June')
    elif month_num == 7:
        print('July')
    elif month_num == 8:
        print('August')
    elif month_num == 9:
        print('September')
    elif month_num == 10:
        print('October')
    elif month_num == 11:
        print('November')
    elif month_num == 12:
        print('December')
else:
    print('Invalid Month Number')
  1. Write a python program to accept the percentage from the user and display the category according to the following criteria: Percentage Category

<40 Failed >=40 and <55 Fair >= 55 and <65 Good >= 65 Excellent

Solution
# take input from user
percentage = float(input("Enter the percentage: "))

# check the category based on the percentage
if percentage < 40:
    print("Failed")
elif percentage >= 40 and percentage < 55:
    print("Fair")
elif percentage >= 55 and percentage < 65:
    print("Good")
else:
    print("Excellent")
  1. Take three sides (number) from the user and tell whether the triangle is isosceles, scalene and equilateral triangle.

Solution
# take input from user
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))

# check if it forms a triangle
if a + b > c and a + c > b and b + c > a:
    # check for equilateral triangle
    if a == b == c:
        print("The triangle is an equilateral triangle.")
    # check for isosceles triangle
    elif a == b or b == c or a == c:
        print("The triangle is an isosceles triangle.")
    # otherwise, it must be a scalene triangle
    else:
        print("The triangle is a scalene triangle.")
else:
    print("These sides do not form a triangle.")

  1. Alice, Bob and Charlie are bidding on an artifact at an auction.

Alice bids A rupees, Bob bids B rupees and Charlie bids C rupees (where A, B and C are distinct).

According to the rule of the auction, the one who bids the highest amount will win the auction. Determine who will win the auction.

Input:

Take input of alice, bob and Charlie’s amount.

Output:

Print the name of auctioner who wins the auction.

Solution
# take input from user
a = float(input("Enter Alice's bid: "))
b = float(input("Enter Bob's bid: "))
c = float(input("Enter Charlie's bid: "))

# check who has the highest bid
if a > b and a > c:
    print("Alice wins the auction!")
elif b > a and b > c:
    print("Bob wins the auction!")
elif c > a and c > b:
    print("Charlie wins the auction!")
else:
    print("There is no clear winner. Two or more people have bid the same highest amount.")
#else condition have been added despite of A,B and C
#being distinct as specified by question
#but it is just for clear understanding of the program.
PreviousString CheatSheetNextLoops Assignments

Last updated 1 year ago