Maths
Built-in Math
x % y
x + y
x - y
x * y
x / y
x // y
x**y
round(x, y)
addition
subtraction
multiplication
division
integer division
remainder from integer division
round x to y decimal places
indices (powers)
Built-in Math
Remember to use information provided by the user, you MUST convert the string [from input()] to either integer [int()] or float [float()] format.
num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
division = num1 / num2 #This will produce an error
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
division = num1 / num2 #This will run correctly
Math Module
import math
math.pi
math.sqrt(num)
import math
pi = math.pi
root = math.sqrt(num)
from math import pi, sqrt
pi = math.pi
root = math.sqrt(num)
For more information about the math module go to:
from math import *
Tasks I
- Ask the user to enter a number with lots of decimal places. Multiply this number by 2 and display the answer.
- Update program 1 so that it will display the answer to two decimal places.
- Ask the user to enter an integer that is over 500. Work out the square root of that number and display it to two decimal places.
- Display pi () to 5 decimal places.
- Ask the user to enter the radius of a circle (measurement from a centre point to the edge). Work out the area of the circle (pi x radius squared).
Tasks II
- Ask for the radius and depth of a cylinder and work out the total volume (circle area x depth) rounded to 3 decimal places.
- Ask the user to enter two numbers. Use whole number division to divide the first number by the second and also work out the remainder and display the answer in a user-friendly way (e.g. if they are 7 and 2 display "7 divided by 2 is 3 with 1 remaining").
Tasks III
-
Display the following message:
If the user enters 1, then it should ask them for the length of one of its sides and display the area. If they select 2, it should ask for the base and height of the triangle and display the area. If they type in anything else, it should give them a suitable error message.
1) Square
2) Triangle
Enter a number:
D Maths
By David James
D Maths
Python - Maths
- 521