Start free

GCSE · 22 exercises

Python arithmetic

input() always gives text: turn it into a whole number with int() or a decimal with float() before doing sums.

+ - * / work as in maths; / always gives a decimal. // divides and drops the remainder, % gives the remainder, ** raises to a power.

a = int(input())
b = int(input())
print(a + b)
print(a // b, a % b)

Watch for: "5" + "3" is "53", not 8: if a sum joins numbers together, an int() is missing.

Exercises

  1. Sum of two numbersRead two whole numbers and print their sum.Novice
  2. DifferenceRead two whole numbers a and b and print a - b.Novice
  3. ProductRead two whole numbers and print their product.Novice
  4. Square and cubeRead a whole number n and print its square and its cube.Novice
  5. Rectangle area and perimeterRead the width and height of a rectangle and print its area and its perimeter.Novice
  6. Average of threeRead three whole numbers and print their average with exactly two decimals.Novice
  7. Whole division and remainderRead two positive whole numbers a and b.Novice
  8. Minutes to hoursRead a number of minutes and say how many hours and minutes that is.Novice
  9. Seconds to a clockRead a number of seconds (less than one day) and print it as hours, minutes andNovice
  10. Celsius to FahrenheitRead a temperature in degrees Celsius and convert it to Fahrenheit:Novice
  11. Kilometres to milesOne kilometre is 0.621371 miles.Novice
  12. Last digitRead a positive whole number and print its last digit.Novice
  13. Tens digitRead a positive whole number and print its tens digit (the second digit from theNovice
  14. Sum of three digitsRead a three-digit number and print the sum of its digits.Novice
  15. Circle areaRead the radius of a circle and print its area with two decimals.Novice
  16. PowerRead two whole numbers a and b (b is not negative) and print a to the power of b.Novice
  17. ChangeA shop sells something for a price; the customer pays with a larger note.Novice
  18. Split the billFriends share a restaurant bill equally.Novice
  19. Pizza slicesSome pizza slices are shared equally.Novice
  20. Average speedRead a distance in kilometres and a time in hours, and print the average speed inNovice
  21. Round itRead a number with decimals and print it three ways, one per line: rounded to theNovice
  22. Remainder by 2Read a whole number and print the remainder when it is divided by 2.Novice
Start with “Sum of two numbers”