Manu py set 3
Manu py set 3
Practical 3
1. Write a program that generates and prints 50 random integers, each between 3 and 6.
import random
random_numbers = [random.randint(3, 6) for _ in
range(50)] print(random_numbers)
O/P: [3, 6, 3, 4, 3, 4, 3, 5, 5, 5, 5, 5, 3, 6, 4, 5, 4, 5, 4, 3, 3, 3, 4, 3, 6, 5, 6, 5, 5, 5, 4, 6, 3, 6, 6, 5, 3, 4, 3, 3, 5, 5, 6, 3, 5, 4, 4, 3, 6, 5]
2. Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x y .
import random
x = random.randint(1,
50) y =
random.randint(2, 5)
result = x ** y
print(f"x*y = {result}")
3. Write a program that generates a random number between 1 and 10 and prints your name that many times.
import random
num = random.randint(1, 10)
print(num)
for i in range(num):
print("Universe")
O/P: 2
Universe
Universe
4. Write a program that generates a random decimal number between 1 and 10 with two decimal places of accuracy. Examples are 1.23,
3.45, 9.80, and 5.00.
import random
random_decimal = round(random.uniform(1, 10), 2)
print(random_decimal)
O/P: 5.56
5. Write a program that generates 50 random numbers such that the first number is between 1 and 2, the second is between 1 and 3, the
third is between 1 and 4, . . . , and the last is between 1 and 51.
import random
6. Write a program that asks the user to enter two numbers, x and y, and computes |x−y/x+y .
7. Write a program that asks the user to enter an angle between −180◦ and 180◦ . Using an expression with the modulo operator,
convert the angle to its equivalent between 0 ◦ and 360◦
8. Write a program that asks the user for a number of seconds and prints out how many minutes and seconds that is. For instance,
200 seconds is 3 minutes and 20 seconds. [Hint: Use the // operator to get minutes and the % operator to get seconds.]
9. Write a program that asks the user for an hour between 1 and 12 and for how many hours in the future they want to go. Print out
what the hour will be that many hours into the future. An example is shown below. Enter hour: 8 How many hours ahead? 5 New
hour: 1 o'clock
10. One way to find out the last digit of a number is to mod the number by 10. Write a program that asks the user to enter a power.
Then find the last digit of 2 raised to that power.
11. Write a program that asks the user to enter a weight in kilograms. The program should convert it to pounds, printing the
answer rounded to the nearest tenth of a pound
12. Write a program that asks the user for a number and prints out the factorial of that number
13. Write a program that asks the user for a number and then prints out the sine, cosine, and tangent of that number
import math
num = float(input("Enter a number in radians: "))
sin_result = math.sin(num)
cos_result = math.cos(num)
tan_result = math.tan(num)
print(f"The sine of {num} is {sin_result}.")
print(f"The cosine of {num} is {cos_result}.")
print(f"The tangent of {num} is
{tan_result}.")
14. Write a program that asks the user to enter an angle in degrees and prints out the sine of that angle.
import math
angle_degrees = float(input("Enter an angle in degrees: "))
angle_radians = math.radians(angle_degrees)
sin_result = math.sin(angle_radians)
print(f"The sine of {angle_degrees} degrees is {sin_result}.")
15. Write a program that prints out the sine and cosine of the angles ranging from 0 to 345◦ in 15◦ increments. Each result should be
rounded to 4 decimal places. Sample output is shown below: 0 --- 0.0 1.0 15 --- 0.2588 0.9659 30 --- 0.5 0.866 ... 345----0.2588 0.9659
import math
for angle_degrees in range(0, 346, 15):
angle_radians = math.radians(angle_degrees)
sin_result = round(math.sin(angle_radians), 4)
cos_result = round(math.cos(angle_radians), 4)
print(f"{angle_degrees} {sin_result} {cos_result}")
17. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400.
Ask the user to enter a year, and, using the // operator, determine how many leap years there have been between 1600 and that
year. def count_leap_years(year):
count = 0
En.no.: 22171341203 Batch: CE54
Name: Emmanuel Zurumba
18. Write a program that given an amount of change less than $1.00 will print out exactly how many quarters, dimes, nickels, and
pennies will be needed to efficiently make that change. [Hint: the // operator may be useful.]
def calculate_change(amount):
19. Write a program that draws “modular rectangles” like the ones below. The user specifies the width and height of the rectangle, and
the entries start at 0 and increase typewriter fashion from left to right and top to bottom, but are all done mod 10. Below are examples of
a 3 × 5 rectangle and a 4 × 8.