xxxxxxxxxx
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
xxxxxxxxxx
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
xxxxxxxxxx
#math in python: Multiple action
x = int(input("Type value for x: "))
y = int(input("Type value for y: "))
z = int(input("Type value for z: "))
print(x / y + z)
#you need multiple solution at once.
#Go ahead and copy the code to your
# .py script to see the results.
Python has a built-in math module that provides a set of functions for mathematical operations. Here are some commonly used math methods in Python:
abs(): Returns the absolute value of a number.
ceil(): Returns the smallest integer greater than or equal to a number.
floor(): Returns the largest integer less than or equal to a number.
round(): Returns the nearest integer to a number.
sqrt(): Returns the square root of a number.
pow(): Returns the value of a number raised to a specified power.
log(): Returns the natural logarithm of a number.
log10(): Returns the base-10 logarithm of a number.
exp(): Returns the value of e raised to the power of a specified number.
cos(): Returns the cosine of a number in radians.
sin(): Returns the sine of a number in radians.
tan(): Returns the tangent of a number in radians.
acos(): Returns the inverse cosine of a number in radians.
asin(): Returns the inverse sine of a number in radians.
atan(): Returns the inverse tangent of a number in radians.
Here's an example of using some of these methods:
xxxxxxxxxx
import math
# Absolute value
x = -5
print(abs(x))
# Ceil and floor
y = 3.7
print(math.ceil(y))
print(math.floor(y))
# Round
z = 2.6
print(round(z))
# Square root and power
a = 16
print(math.sqrt(a))
print(pow(a, 2))
# Logarithm
b = 10
print(math.log(b))
print(math.log10(b))
# Exponential
c = 2
print(math.exp(c))
# Trigonometric functions
angle = math.pi / 4
print(math.cos(angle))
print(math.sin(angle))
print(math.tan(angle))
print(math.acos(math.sqrt(2)/2))
print(math.asin(math.sqrt(2)/2))
print(math.atan(1))
This will output:
5
4
3
3
4.0
256
2.302585092994046
1.0
7.3890560989306495
0.7071067811865476
0.7071067811865475
0.9999999999999999
0.7853981633974484
0.7853981633974483
0.7853981633974483
xxxxxxxxxx
import math
# Basic operations
addition = 2 + 3
subtraction = 5 - 1
multiplication = 4 * 2
division = 10 / 3 # Returns a float
# Advanced operations
power = pow(2, 3) # Calculates 2^3
square_root = math.sqrt(16)
absolute_value = abs(-7)
rounding = round(4.7)
# Trigonometry
sine = math.sin(math.radians(45))
cosine = math.cos(math.radians(60))
tangent = math.tan(math.radians(30))
# Constants
pi = math.pi
euler_number = math.e
# More complex functions available in the math module
print(f"Addition: {addition}")
print(f"Subtraction: {subtraction}")
print(f"Multiplication: {multiplication}")
print(f"Division: {division}")
print(f"Power: {power}")
print(f"Square Root: {square_root}")
print(f"Absolute Value: {absolute_value}")
print(f"Rounding: {rounding}")
print(f"Sine: {sine}")
print(f"Cosine: {cosine}")
print(f"Tangent: {tangent}")
print(f"Pi: {pi}")
print(f"Euler's number: {euler_number}")
xxxxxxxxxx
Python comes with a built-in math library called `math` which provides various mathematical functions and constants. Here is an example of how you can use the `math` library in Python:
import math
# Calculate the square root of a number
num = 16
sqrt = math.sqrt(num)
print("Square root of", num, "is", sqrt)
# Calculate the factorial of a number
num = 5
factorial = math.factorial(num)
print("Factorial of", num, "is", factorial)
# Calculate the sin of an angle in radians
angle = math.pi / 4
sin = math.sin(angle)
print("Sin of", angle, "is", sin)
# Calculate the logarithm base 10
num = 100
log = math.log10(num)
print("Logarithm base 10 of", num, "is", log)
xxxxxxxxxx
x = int(10)
y = int(5)
print(x + y)
#you need to define integer extra otherwise
# python will count this as a str and won't work.
#copy the code to your py script to see the accurate result.