String Operation and Math Unit Exercises

On this page you will find a range of additional exercises that will help you practice the skills from the previous pages. You may need to investigate the Python Documentation as well...

You will find the solutions to these exercises on your GitHub page.

Using the Math Unit

The Math Unit enables you to use mathematical functions such as π (pi) and sine, cosine and tangent.

You can use it by including the following line at the top of your program:

import math

The following functions may be useful:

  • math.pi - provides an approximation of π
  • math.radians(x) - converts x from degrees to radians.
  • math.sin(x) - returns the sine of x radians.
  • math.cos(x) - returns the cosine of x radians.
  • math.tan(x) - returns the tangent of x radians.

String Operations

There are various functions you can apply to strings that may be helpful (assuming you have a string stored in a variable called yourString):

  • yourString.upper() - returns the string in upper case.
  • yourString.lower() - returns the string in lower case.
  • yourString.capitalize() - returns the string with the first letter of the string capitalized
  • yourString.title() - returns the string with the first letter of each word capitalzed.
  • yourString.replace(x,y) - returns the string with the characters represented by x replaced by the characters represented by y.
  • yourString[x:y] - returns the string starting at character x and ending before character y.

Basic Exercises

  1. Write a program that will ask the user for four integer numbers and then add these numbers together before displaying the answer. The input and output should be user friendly.
  2. Write a program that will ask the user for three integer numbers and then multiply the first two together before dividing the result by the third number. The input and output should be user friendly.
  3. Write a program that will ask the user for two numbers a then divide one by the other. The number of times one goes into another and the remainder should be displayed. For example, If 3 and 2 were entered: 3/2 = 1 remainder 1. The input and output should be user friendly.
  4. Improve the previous program to show only the integer part of the answer.
  5. Both a fridge and a lift have heights, widths and depths. Work out how much space is left in the lift once the fridge is inside. Assume that the fridge dimensions will fit. The input and output should be user friendly.
  6. Write a program that asks the user for the amount of money they will take on holiday and convert this into the equivalent amount in Euros, ignoring any Cents that might result from the conversion. The input and output should be user friendly.
  7. Improve the above program so that it will tell you how many 50,20,10 and 5 Euro notes you would receive for a given value of Pounds.

Math unit exercises

  1. Calculate the circumference and area of a circle when the user enters a radius. Round the answers to 2 decimal places. The input and output should be user friendly.
  2. A circular swimming pool is x metres in diameter. What volume of water does it contain if the pool is the same depth at all points?
  3. A plane is travels 20km on a course heading 60 degrees from north. Write a program that will calculate how far north and how far east has the plane travelled at this point?
  4. Generalise the previous program so that it will work for any distance entered and degrees from north.
  5. A road is inclined at an angle of 1 degree to the horizontal. Find the vertical height climbed when a car travels 1 km up the hill.
  6. The foot of a ladder is 1.5m from a vertical wall. The ladder makes an angle of 68 degrees with the horizontal. How far up the wall does the ladder reach?
  7. The string of a balloon is 120m long and makes an angle of 70 degrees with the horizontal. What is the height of the balloon.

String Exercises

  1. Create a program that will allow the user to enter a quote by a famous person. Output this quote in upper case, lower case, capitalise and title formats.
  2. Improve the program so that the user can replace a one word with another word.
  3. Improve the program so that the user can print out only part of the quote.