math module python code example
Example 1: ptython math methods
math.acos() Returns the arc cosine of a number
math.acosh() Returns the inverse hyperbolic cosine of a number
math.asin() Returns the arc sine of a number
math.asinh() Returns the inverse hyperbolic sine of a number
math.atan() Returns the arc tangent of a number in radians
math.atan2() Returns the arc tangent of y/x in radians
math.atanh() Returns the inverse hyperbolic tangent of a number
math.ceil() Rounds a number up to the nearest integer
math.comb() Returns the number of ways to choose k items from n items without repetition and order
math.copysign() Returns a float consisting of the value of the first parameter and the sign of the second parameter
math.cos() Returns the cosine of a number
math.cosh() Returns the hyperbolic cosine of a number
math.degrees() Converts an angle from radians to degrees
math.dist() Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point
math.erf() Returns the error function of a number
math.erfc() Returns the complementary error function of a number
math.exp() Returns E raised to the power of x
math.expm1() Returns Ex - 1
math.fabs() Returns the absolute value of a number
math.factorial() Returns the factorial of a number
math.floor() Rounds a number down to the nearest integer
math.fmod() Returns the remainder of x/y
math.frexp() Returns the mantissa and the exponent, of a specified number
math.fsum() Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)
math.gamma() Returns the gamma function at x
math.gcd() Returns the greatest common divisor of two integers
math.hypot() Returns the Euclidean norm
math.isclose() Checks whether two values are close to each other, or not
math.isfinite() Checks whether a number is finite or not
math.isinf() Checks whether a number is infinite or not
math.isnan() Checks whether a value is NaN (not a number) or not
math.isqrt() Rounds a square root number downwards to the nearest integer
math.ldexp() Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i
math.lgamma() Returns the log gamma value of x
math.log() Returns the natural logarithm of a number, or the logarithm of number to base
math.log10() Returns the base-10 logarithm of x
math.log1p() Returns the natural logarithm of 1+x
math.log2() Returns the base-2 logarithm of x
math.perm() Returns the number of ways to choose k items from n items with order and without repetition
math.pow() Returns the value of x to the power of y
math.prod() Returns the product of all the elements in an iterable
math.radians() Converts a degree value into radians
math.remainder() Returns the closest value that can make numerator completely divisible by the denominator
math.sin() Returns the sine of a number
math.sinh() Returns the hyperbolic sine of a number
math.sqrt() Returns the square root of a number
math.tan() Returns the tangent of a number
math.tanh() Returns the hyperbolic tangent of a number
math.trunc() Returns the truncated integer parts of a number
Example 2: how to import math in python
import math
Example 3: math pyhon ?
Arithmetic:
operator | name | example |
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
Comparison:
Operator | Name | Example |
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Assignment:
Operator | Example |
= x = 5
+= x += 3
-= x -= 3
*= x *= 3
/= x /= 3
%= x %= 3
//= x //= 3
**= x **= 3
&= x &= 3
|= x |= 3
^= x ^= 3
>>= x >>= 3
<<= x <<= 3
Example 4: tangent python 3
import math
import random
#the tangent can be anything
variable = random.randint(6, 9)
#here we print the tangent with atan (tangent)
print(atan(variable))
Example 5: python mathematics
// Python mathematical functions include but are not limited to:
print(10+10) // Returns 20 (Addition)
print(10*10) // Returns 100 (Multiplication)
print(10/10) // Returns 1 (Division)
print(10**10) // Returns 10000000000 (10 to the power of 10, 10^10)
print(10-10) // Returns 0 (Subtraction)
print(10>100) // Returns False (If A is greater than B)
print(10<100) // Returns True (If A is less than B)
print(10==10) // Returns True (If A is equal to B)
import math // Required for the function below
print(math.pi) // Returns the first 15 decimal places of Pi
Example 6: python math sheet
import time, random, numpy as np
'''note: this is before i leanred the round(num,prec)
^
'''
def space():
'''spaceing'''
time.sleep(0.2)
print('')
time.sleep(0.2)
class test:
def fract():
'''fraction test'''
num = random.randint(1, 6)
if num == 1:
user = float(input('what is 1/3 decimals = 3: '))
if (np.around(1/3, decimals=3)) == user:
print('yes 1/3 = 0.333 or 0.33 and 1/3')
else:
print('no 1/3 = 0.333 or .33 and 1/3')
elif num == 2:
user = float(input('what is 2/3, decimals = 3: '))
if (np.around(2/3, decimals=3)) == user:
print('yes 2/3 = 0.666 or 0.66 and 2/3')
else:
print('no 2/3 = 0.666 or 0.66 and 2/3')
elif num == 3:
user = float(input('what is 1/8, decimals = 3: '))
if (np.around(1/8, decimals=3)) == user:
print('yes 1/8 = 0.125 or 0.12 and 1/2')
else:
print('no 2/3 = 0.125 or 0.12 and 1/2')
elif num == 4:
user = float(input('what is 3/8, decimals = 3: '))
if (np.around(3/8, decimals=3)) == user:
print('yes 3/8 = 0.375 or 0.37 and 1/2')
else:
print('no 3/8 = 0.375 or 0.37 and 1/2')
elif num == 5:
user = float(input('what is 5/8, decimals = 3: '))
if (np.around(5/8, decimals=3)) == user:
print('yes 5/8 = 0.625 or 0.62 and 1/2')
else:
print('no 5/8 = 0.625 or 0.62 and 1/2')
elif num == 6:
user = float(input('what is 7/8, decimals = 3: '))
if (np.around(7/8, decimals=3)) == user:
print('yes 3/8 = 0.875 or 0.87 and 1/2')
else:
print('no 5/8 = 0.875 or 0.87 and 1/2')
def div(a, b):
correct = a/b
user = int(input(f'what is {a} / {b}: '))
if user == correct:
print(f'yes {a} / {b} = {correct}')
else:
print(f'no {a} / {b} = {correct}')
def mul(a, b):
anc = a*b
user = int(input(f'{a} * {b} = : '))
if user == a*b:
print(f'correct {a} * {b} = {a*b}')
else:
print(f'not corect {a} * {b} = {a*b}')