Python For GCSE
Python has some built-in mathematical statements and a built-in module with a load more. These come in useful in all sorts of programs.
The following program shows some of the mathematical statements that are automatically available to you without needing to import anything.
small = min(1, 10, 3, 7) print("The smallest value is", small) large = max(1, 10, 3, 7) print("The largest value is", large) a = abs(-23) print("The absolute value of -23 is", a) p = pow(3,4) print("3 to the power of 4 is", p)
If you import the math module, you get access to a load of other methods. Some of the ones you might use up to GCSE are shown below.
If you use any trigonometric methods, bear in mind that they work with radians instead of degrees. Spend some time learning about how radians work before you use these methods.
|math.acos()||Returns the arccosine of a number||math.acos(0.5)|
|math.asin()||Returns the arcsine of a number||math.asin(0.5)|
|math.atan()||Returns the arctangent of a number in radians||math.atan(0.5)|
|math.ceil()||Rounds a number up to the nearest integer||math.ceil(1.3)|
|math.comb()||Returns the number of ways to choose k items from n items without repetition and order||math.comb(10, 2)|
|math.cos()||Returns the cosine of a number||math.cos(0)|
|math.degrees()||Converts an angle from radians to degrees||math.degrees(1)|
|math.exp()||Returns E raised to the power of x||math.exp(10)|
|math.factorial()||Returns the factorial of a number||math.factorial(5)|
|math.floor()||Rounds a number down to the nearest integer||math.floor(12.45)|
|math.gcd()||Returns the greatest common divisor of two integers||math.gcd(16, 4)|
|math.log()||Returns the natural logarithm of a number, or the logarithm of number to base||math.log(2)|
|math.log10()||Returns the base-10 logarithm of x||math.log10(100)|
|math.log2()||Returns the base-2 logarithm of x||math.log2(128)|
|math.perm()||Returns the number of ways to choose k items from n items with order and without repetition||math.perm(10, 2)|
|math.radians()||Converts a degree value into radians||math.radians(180)|
|math.sin()||Returns the sine of a number||math.sin(0)|
|math.sqrt()||Returns the square root of a number||math.sqrt(16)|
|math.tan()||Returns the tangent of a number||math.tan(1)|
|math.trunc()||Returns the truncated integer parts of a number||math.trunc(1.74)|
The math module also includes some useful constants. The most useful of these is probably math.pi. Another one you might find a use for is math.tau (which is 2 times pi). Euler's constant can be returned with math.e.