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