Type C: Programming Practice/Knowledge-based Questions
1. Write a program to print one of the words negative, zero, or positive, according to whether variable x is less than zero, zero, or greater than zero, respectively.
#Que - 1
x = int(input("Enter a Number : "))
if x < 0:
print("Negative")
elif x == 0:
print("Zero")
else:
print("Postitive")
Output:
Enter a Number: -5
Negative
Enter a Number: 0
Zero
Enter a Number: 8
Positive
2. Write a program that returns True if an input number is an even number, and False otherwise.
#Que - 2
x = int(input("Enter a Number : "))
if x % 2 == 0:
print("Even Number => ", True)
else:
print("Even Number => ", False)
Output:
Enter a Number: 9
Even Number => False
Enter a Number: 8
Even Number => True
3. Write a Python program that calculates and prints the number of seconds in a year.
#Que - 3
year = int(input("Enter Year : "))
if (year % 4 == 0 and year %100 != 0) or (year % 100 == 0 and year % 400 == 0):
no_sec = 366 * 24 * 60 * 60
print(year, "Leap year")
else:
no_sec = 365 * 24 * 60 * 60
print(year, "Not a Leap year")
print("Number of seconds in year", year,"is",no_sec)
Output:
Enter Year: 1900
1900 Not a Leap year
Number of seconds in year 1900 is 31536000
Enter Year : 2000
2000 Leap year
Number of seconds in year 2000 is 31622400
Enter Year : 2022
2022 Not a Leap year
Number of seconds in year 2022 is 31536000
4. Write a Python program that accepts two integers from user and prints a message saying if first number is divisible by second number or if it is not.
#Que - 4
x = int(input("Enter Number 1 : "))
y = int(input("Enter Number 2 : "))
if x % y == 0:
print("First number", x,"is divisible by second number", y)
else:
print("Not divisible")
Output:
Enter Number 1 : 9
Enter Number 2 : 3
First number 9 is divisible by second number 3
Enter Number 1 : 15
Enter Number 2 : 4
Not divisible
5. Write a program that asks the day number in a year in the range 2 to 365 and asks the first day of the year – Sunday or Monday or Tuesday etc. Then the program should display the day on the day number that has been input.
#Que - 5
day_number = int(input("Input Day number ranges 2 - 365 : "))
first_day_name = int(input("First Day of the year (0 to 6) i.e 0 - Sunday, 1 - Monday ... 6 - Saturday: "))
day_name = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
new_day = (day_number % 7 + first_day_name - 1)%7
print("The day on the ", day_number,"is ", day_name[new_day])
Output:
Input Day number ranges 2 – 365 : 6
First Day of the year (0 to 6) i.e 0 – Sunday, 1 – Monday … 6 – Saturday: 1
The day on the 6 is Saturday
Input Day number ranges 2 – 365 : 35
First Day of the year (0 to 6) i.e 0 – Sunday, 1 – Monday … 6 – Saturday: 3
The day on the 35 is Tuesday
6. One foot equals 12 inches. Write a function that accepts a length written in feet as an argument and returns this length written in inches. Write a second function that asks the user for a number of feet and returns this value. Write a third function that accepts a number of inches and displays this to the screen. Use these three functions to write a program that asks the user for a number of feet and tells them the corresponding number of inches.
#Que - 6
def feet_to_inch(foot):
return foot * 12
def input_feet():
foot = int(input("Enter value in foot : "))
return foot
def print_inch(inch):
print("Value in inch : ",inch)
feet = input_feet()
inch = feet_to_inch(feet)
print_inch(inch)
Output:
Enter value in foot : 12
Value in inch : 144
Enter value in foot : 19
Value in inch : 228
7. Write a program that reads an integer N from keyboard computes and displays sum of numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it’s the sum of numbers from (2 * N) to N. The starting and ending points are included in the sum.
#Que - 7
N = int(input("Enter a number : "))
Sum = 0
print("Sum of =>")
if N > 0:
for num in range(N, 2*N + 1):
print(num, end=" ")
Sum = Sum + num
elif N < 0:
for num in range(2*N, N + 1):
print(num, end=" ")
Sum = Sum + num
print(" = ",Sum)
Output:
Enter a number : 5
Sum of =>
5 6 7 8 9 10 = 45
Enter a number : -5
Sum of =>
-10 -9 -8 -7 -6 -5 = -45
8. Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a function that prints print out the date in the format <Month Name> <day>, <year>.
Sample run: Enter date: 12252019
December 25, 2019
#Que - 8 : Method - 1
month = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December']
dt = input("Enter Date in Integer MMDDYYY : ")
mm = dt[:2]
dd = dt[2:4]
yy = dt[4:]
print(month[int(mm)-1], dd,",",yy)
Enter Date in Integer MMDDYYY : 12022022
December 02 , 2022
#Que - 8 - Method 2
import datetime
DateInInt = input("Enter Date in Integer MMDDYYYY : ")
d = datetime.datetime.strptime(DateInInt,'%m%d%Y')
print(d.strftime('%B'), d.strftime('%d'),",",d.strftime('%Y'))
Output:
Enter Date in Integer MMDDYYY: 12022022
December 02 , 2022
9. Write a program that prints a table on two columns —table that helps converting miles
#Que - 9
print("Miles - Kilometer")
print("-----------------")
for m in range(1,10):
print(m, '-', m * 1.60934)
print("-----------------")
Output:
Miles – Kilometer
1 – 1.60934
2 – 3.21868
3 – 4.82802
4 – 6.43736
5 – 8.0467
6 – 9.65604
7 – 11.26538
8 – 12.87472
9 – 14.48406
10. Write another program printing a table with two columns that helps convert pounds in kilograms
#Que - 10
print("Pounds - Kilogram")
print("-----------------")
for m in range(1,10):
print(m, '-', m * 0.453592)
print("-----------------")
Output:
Pounds – Kilogram
1 – 0.453592
2 – 0.907184
3 – 1.360776
4 – 1.814368
5 – 2.26796
6 – 2.721552
7 – 3.175144
8 – 3.628736
9 – 4.082328
11. Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. A sample run is being given:
Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes
Method – 1
#Que - 11
#Two times in military format
start = input("enter start time in millitary format : ")
end = input("enter end time in millitary format :")
start_hh = int(start[0:2]) # extract hour part from start time
start_mm = int(start[2:]) # extract minute part from start time
end_hh = int(end[0:2]) # extract hour part from end time
end_mm = int(end[2:]) # extract hour part from end time
if end_mm < start_mm: # check if end minute is less than the start minute
end_hh = end_hh - 1 # subtract 1 from end hour
end_mm = end_mm + 60 #add 60 to end minute
hh = end_hh - start_hh # differece of hour
mm = end_mm - start_mm # difference of minute
print(hh, "hours", mm,"minutes")
Output:
enter start time in millitary format : 0900
enter end time in millitary format :1730
8 hours 30 minutes
enter start time in millitary format : 0950
enter end time in millitary format :1115
1 hours 25 minutes
Method – 2
#Que - 11
#Two times in military format
import datetime
firsttime = input("Please enter the first time : ")
ft = datetime.datetime.strptime(firsttime,'%H%M')
secondtime = input("Please enter the second time : ")
st = datetime.datetime.strptime(secondtime,'%H%M')
tdiff = st - ft #tdiff is a timedelta object
seconds = tdiff.total_seconds() #returns the time in seconds
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
print(hours,"hours", minutes,"minutes")
Output:
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Please enter the first time : 1530
Please enter the second time : 2140
6 hours 10 minutes
Ch. 2- Python Revision Tour – II Solution of Sumita Arora