0% found this document useful (0 votes)
4 views9 pages

BCP 2-1

This document contains a series of Python programming exercises focused on operators and control statements. Each exercise includes a problem statement, corresponding code, and sample output, covering topics such as mathematical expressions, user input, conditional statements, and calculations related to distance, time, and bills. The exercises aim to demonstrate practical applications of Python programming concepts.

Uploaded by

vishalkanade6959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

BCP 2-1

This document contains a series of Python programming exercises focused on operators and control statements. Each exercise includes a problem statement, corresponding code, and sample output, covering topics such as mathematical expressions, user input, conditional statements, and calculations related to distance, time, and bills. The exercises aim to demonstrate practical applications of Python programming concepts.

Uploaded by

vishalkanade6959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Programs - Operators and Control Statements

Experiment No. 2

Python program to demonstrate usage of operators and control


statements.

Name: Dhruv Naik​


Branch: Electronics and Computer Engineering​
Roll No: 24EC07

1. Write a Python program to implement the following expression. Ask


the user to enter the values of each variable. Print the answer

Code:

X= float(input("ENTER VALUE OF X "))​


Y= float(input("ENTER VALUE OF Y "))​
Z= float(input("ENTER VALUE OF Z "))​
C= float(input("ENTER VALUE OF C "))​
D= float(input("ENTER VALUE OF D "))​

ans=((2*X*Y)/(C+10))-((X)/(4*(Z+D)))​

print("ANSWER ::",ans)

Output:

ENTER VALUE OF X 5​
ENTER VALUE OF Y 5​
ENTER VALUE OF Z 5​
ENTER VALUE OF C 5​
ENTER VALUE OF D 5​
ANSWER :: 3.2083333333333335
2.Write a Python program to implement the following expression. Ask
the user to enter the values of each variable. Print the answer

Code:

a= float(input("ENTER VALUE OF a "))​


b= float(input("ENTER VALUE OF b "))​
C= float(input("ENTER VALUE OF C "))​
d= float(input("ENTER VALUE OF d "))​
x= float(input("ENTER VALUE OF x "))​
Y= float(input("ENTER VALUE OF Y "))​
z= float(input("ENTER VALUE OF z "))​

ans=((((10*Y)*(a*b+C))/d)-0.8+2*b)/((x+a)*(1/z))​

print("ANSWER ::",ans)

Output:

ENTER VALUE OF a 5​
ENTER VALUE OF b 5​
ENTER VALUE OF C 5​
ENTER VALUE OF d 5​
ENTER VALUE OF x 5​
ENTER VALUE OF Y 5​
ENTER VALUE OF z 5​
ANSWER :: 154.6

3. Write a program to read the marks of 5 subjects through the


keyboard. Find out the aggregate and percentage of marks obtained by
the student. Assume maximum marks that can be obtained by a student
in each subject as 100.
Code:

a= float(input("ENTER MARKS FOR SUB1 ::"))​


b= float(input("ENTER MARKS FOR SUB2 ::"))​
c= float(input("ENTER MARKS FOR SUB3 ::"))​
d= float(input("ENTER MARKS FOR SUB4 ::"))​
e= float(input("ENTER MARKS FOR SUB5 ::"))​

total=a+b+c+d+e​
P=total/5​

print("AGGREGATE ::",total)​
print("PERCENTAGE ::",P)

Output:

ENTER MARKS FOR SUB1 ::90​


ENTER MARKS FOR SUB2 ::80​
ENTER MARKS FOR SUB3 ::70​
ENTER MARKS FOR SUB4 ::60​
ENTER MARKS FOR SUB5 ::50​
AGGREGATE :: 350.0​
PERCENTAGE :: 70.0

4. Write a program to read the distance between any two cities in


kilometer (km) and print the distances in meters (m), centimeters
(cm)and miles. Note: 1 km = 1000 meter 1 km = 100000 centimeter 1
km = 0.6213 miles

Code:

D= float(input("ENTER DISTANCE IN KM ::"))​



print("DISTANCE IN METERS ::",D*1000)​
print("DISTANCE IN CENTIMETERS ::",D*100000)​
print("DISTANCE IN MILES ::",D*0.6213)
Output:

ENTER DISTANCE IN KM ::21​


DISTANCE IN METERS :: 21000.0​
DISTANCE IN CENTIMETERS :: 2100000.0​
DISTANCE IN MILES :: 13.04879

5. An ATM contains Indian currency notes of 100, 500 and 2000. To


withdraw cash from this ATM, the user has to enter the amount which is
a multiple of 100. Write a program that will print the number of notes of
each denomination that will be dispensed by the machine

Code:

amount= int(input("ENTER THE AMOUNT ::"))​



n_2000=amount//2000​
amount%=2000​
n_500=amount//500​
amount%=500​
n_100=amount//100​

print(f"2000s: {n_2000}\n500s: {n_500}\n100s: {n_100}")

Output:

ENTER THE AMOUNT ::2600​


2000s: 1​
500s: 1​
100s: 1

6. Write a program to Input Departure time in Hr (0-23) and in Minutes


(0-60). Also input the trave time in minutes. Show the arrival time in
hrs(0-23) and minutes (0-60)
Code:

d_hr= int(input("ENTER DEPARTURE HOUR(0-23) ::"))​


d_min= int(input("ENTER DEPARTURE MINUTES(0-60) ::"))​
tt=int(input("ENTER TRAVEL TIME IN MINUTES(0-60) ::"))​

total=d_hr*60+d_min+tt​

print(f"ARRIVAL TIME {(total//60)%24}:{total%60}")

Output:

ENTER DEPARTURE HOUR(0-23) ::21​


ENTER DEPARTURE MINUTES(0-60) ::50​
ENTER TRAVEL TIME IN MINUTES(0-60) ::20​
ARRIVAL TIME 22:10

7. Write a program that reads in an integer and prints whether the


integer is even or not. Remember, a number is even if the number is
divisible by 2. To test this use number % 2 == 0. Ex: If the input is 6, the
output is "6 is even: True".

Code:

a= int(input("ENTER AN INTEGER ::"))​



if a%2==0:​
print("NUMBER IS EVEN")​
else:​
print("NUMBER IS ODD")

Output:

ENTER AN INTEGER ::22​


NUMBER IS EVEN
8. Write a program that reads in a car's speed as an integer and checks if
the car's speed is within the freeway limits. A car's speed must be at
least 45 mph but no greater than 70 mph on the freeway. If the speed is
within the limits, print "Good driving". Else, print "Follow the speed
limits".

Code:

speed = int(input("Enter car speed: "))​



if 45 <= speed <= 70:​
print("Good driving")​
else:​
print("Follow the speed limits")

Output:

Enter car speed: 80​


Follow the speed limits

9. Write a program that reads in an integer representing a visible light


wavelength in nanometers. Print the corresponding color using the
following inclusive ranges: • Violet: 380–449 • Blue: 450–484 • Cyan:
485–499 • Green: 500–564 • Yellow: 565–589 • Orange: 590–624 • Red:
625–750

Code:

wavelength = int(input("Enter wavelength in nm: "))​



if 380 <= wavelength <= 449:​
print("Violet")​
elif 450 <= wavelength <= 484:​
print("Blue")​
elif 485 <= wavelength <= 499:​
print("Cyan")​
elif 500 <= wavelength <= 564:​
print("Green")​
elif 565 <= wavelength <= 589:​
print("Yellow")​
elif 590 <= wavelength <= 624:​
print("Orange")​
elif 625 <= wavelength <= 750:​
print("Red")​
else:​
print("Wavelength out of range")

Output:

Enter wavelength in nm: 500​


Green

10. Write a program that reads in a string, "lunch" or "dinner",


representing the menu choice, and an integer, 1, 2, or 3, representing the
user's meal choice. The program then prints the user's meal choice.
Lunch Meal Options 1. Caesar salad 2. Spicy chicken wrap 3. Butternut
squash soup Dinner Meal Options 1. Baked salmon 2. Turkey burger 3.
Mushroom risotto Ex: If the input is: lunch 3 The output is: Your order:
Butternut squash soup

Code:

meal = input("Enter meal type (lunch/dinner): ").lower()​


choice = int(input("Enter meal choice (1-3): "))​

if meal == "lunch":​
meals = ["Caesar salad", "Spicy chicken wrap", "Butternut squash
soup"]​
elif meal == "dinner":​
meals = ["Baked salmon", "Turkey burger", "Mushroom risotto"]​
else:​
meals = []​

if 1 <= choice <= 3:​
print("Your order:", meals[choice - 1])​
else:​
print("Invalid choice")

Output:

Enter meal type (lunch/dinner): lunch​


Enter meal choice (1-3): 3​
Your order: Butternut squash soup

11. Write a program that reads in an integer, ping, and prints


ping_report, a string indicating whether the ping is low to average or too
high. ping values under 150 have a ping_report of "low to average". ping
values of 150 and higher have a ping_report of "too high". Use a
conditional expression to assign ping_report. Ex: If the input is 30, the
output is "Ping is low to average".

Code:

ping = int(input("Enter ping value: "))​



report = "low to average" if ping < 150 else "too high"​
print("Ping is", report)

Output:

Enter ping value: 30​


Ping is low to average

12. Write a program to calculate an Internet browsing bill. Use the


conditions specifi ed as follows: a. 1 Hour – `20 b. ½ Hour – `10 c.
Unlimited hours in a day – `100
Code:

hours = float(input("Enter hours used: "))​



if hours == 0.5:​
bill = 10​
elif hours == 1:​
bill = 20​
else:​
bill = 100​

print("Your bill is:", bill)

Output:

Enter hours used: 2​


Your bill is: 100

13. Write a program to prompt (input) year and check if it is a leap year

Code:

year = int(input("Enter a year: "))​



if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):​
print(year, "is a leap year")​
else:​
print(year, "is not a leap year")

Output:

Enter a year: 2024​


2024 is a leap year

You might also like