12. Exercises

  1. Evaluate the following numerical expressions in your head, then use the active code window to check your results:

    1. 5 ** 2
    2. 9 * 5
    3. 15 / 12
    4. 12 / 15
    5. 15 // 12
    6. 12 // 15
    7. 5 % 2
    8. 9 % 5
    9. 15 % 12
    10. 12 % 15
    11. 6 % 6
    12. 0 % 7
    1. 5 ** 2  = 25
    2. 9 * 5 = 45
    3. 15 / 12 = 1.25
    4. 12 / 15 = 0.8
    5. 15 // 12 = 1
    6. 12 // 15 = 0
    7. 5 % 2 = 1
    8. 9 % 5 = 4
    9. 15 % 12 = 3
    10. 12 % 15 = 12
    11. 6 % 6 = 0
    12. 0 % 7 = 0
  2. What is the order of the arithmetic operations in the following expression? Evaluate the expression by hand and then check your work.

    2 + (3 - 1) * 10 / 5 * (2 + 3)
    
  3. Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight).

    If it is currently 13 and you set your alarm to go off in 50 hours, the hour will be 15 (3pm). Write a program to solve the general version of the above problem. Ask the user for the current time (in hours), and then ask for the number of hours to wait for the alarm.

    Your program should output what the hour will be on the clock when the alarm goes off.

  4. Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

  5. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.

  6. The formula for computing the final amount if one is earning compound interest is given on Wikipedia as

    formula for compound interest

    Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years.

  7. Write a program that will compute the area of a circle. Prompt the user to enter the radius, and then print the answer, like this:

    What is the radius of your circle?
    >>> 7.8
    191.0376
    
  8. Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.

  9. Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer, like this:

    How many miles have you driven?
    >>> 150
    How many gallons have you used?
    >>> 5
    Your car gets 30 miles per gallon.
    
  10. Write a program that will convert degrees Celsius to degrees Fahrenheit.

  11. Write a program that will convert degrees Fahrenheit to degrees Celsius, like this:

    What is the temperature in Fahrenheit?
    >>> 32
    32.0 degrees Fahrenheit is 0.0 degrees Celsius.