Chapter 01 - Python Revision Tour

Python Revision Tour

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.
user_input=int(input('Please enter a number: '))
if user_input==0:
    print('Zero')
elif user_input<0: egative="" elif="" print="" user_input="">0:
    print('Positive')
2. Write a program that returns True if the input number is an even number otherwise False.
user_input=int(input('Please enter a number: '))
if user_input%2==0:
    print('True')
else:
    print('False')   
3. Write a Python program that calculates and prints the number of seconds in a year.
user_input=int(input('Please enter a Year: '))
if user_input%4==0:
    sec=366*24*60*60
    print('Number of seconds in year',user_input,'is',sec,'seconds')
else:
    sec=365*24*60*60
    print('Number of seconds in year',user_input,'is',sec,'seconds')    
4. Write a Python program that accepts two integers from the user and prints a message saying if first number is divisible by second number or if it is not.
integerOne=int(input('Enter first number: '))
integerTwo=int(input('Enter second number: '))
if integerOne%integerTwo==0:
    print(integerOne,'is divisible by',integerTwo)
else:
    print(integerOne,'is not divisible by',integerTwo)       
5. Write a program that asks the user 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.
day_number=int(input('Enter day number in range 2 to 365: '))
user_input=input('Enter 1st day of the year: ')
day_list=['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
day=day_list.index(user_input)
for i in range(2,day_number):    
    day=day+1
    if day==6:
        day=0
print(day_list[day])
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.
def feet_to_inches(length_in_feet):
    return length_in_feet*12
def input_value():
    length_in_feet=int(input('Enter number in feet: '))
    return length_in_feet
def display(length_in_inches):
    print('Number of inches: ',length_in_inches, 'inch')
length=feet_to_inches(input_value())
inches=feet_to_inches(length)
display(inches) 
7. Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2*N) if N is non-negative. If  N is a negative number, then it's sum of the numbers from (2*N) to N. The starting and ending points are included in the sum.
N=int(input('pleae enter a number: '))
sum=0
if N>0:
    for i in range(N,2*N+1):
        sum=sum+i
else:
    for i in range(2*N,N+1):
        sum=sum+i
print(sum)       
8. Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a function that prints out the date in the format <Month Name> <day>, <year>.
user_input=str(input('Please enter a date in the format MMDDYYYY eg. 12252019: '))
month=int(user_input[0:2])
day=user_input[2:4]
year=user_input[4:]
month_name_list=['January', 'February', 'March', 'April', 'May', 'June', 'July',
 'August', 'September', 'October', 'November', 'December']
month_name=''

for i in range(len(month_name_list)+1):
    if month==i:
        month_name=month_name_list[i-1]    
print(month_name + ' ' + day + ', ' + year)    
9. Write a program that prints a table on two columns - table that helps converting miles into kilometers.
print('Miles(mi)'+'\t\t\t'+'Kilometers(km)\n')
for i in range(1,10):
    print('0'+str(i),' mi\t\t\t',i*1.609344,' km')
for i in range(10,100,10):
    print(i,' mi\t\t\t',i*1.609344,' km')
print('100 mi\t\t\t',100*1.609344,' km')       
10. Write another program printing a table with two columns that helps convert pounds in kilograms.
print('Pounds(lbs)'+'\t\t\t'+'Kilograms(kgs)\n')
for i in range(1,10):
    print('0'+str(i),' lb\t\t\t',i*0.45359237 ,' kg')
for i in range(10,100,10):
    print(i,' lb\t\t\t',i*0.45359237 ,' kg')
print('100 lb\t\t\t',100*0.45359237 ,' kg')
  
           
11. Write a program that rads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times.
first_number=input('please enter 1st number: ')
second_number=input('please enter 2nd number: ')
hours_1st=int(first_number[0:2])
hours_2nd=int(second_number[0:2])
minutes_1st=int(first_number[2:])
minutes_2nd=int(second_number[2:])
hours=abs(hours_1st-hours_2nd)
minutes=abs(minutes_1st-minutes_2nd)
print(hours,'hours',minutes,'minutes')        

Comments

  1. A lot of thanks for Python Notes and nice explanation

    ReplyDelete
  2. This is something u can't find easily ,
    So,A very great thank u😊😊

    ReplyDelete

Post a Comment

Popular posts from this blog

Chapter 05 - File Handling

Dictionary (Python)

Tuple (Python)