a = 12
b = 5
print(a + b) # 17 , sum of two numbers
print(a - b) # 7 , subtraction of two numbers
print(a * b) # 60, product of two numbers
print(a / b) # 2.4, quotient of two numbers
print(a // b) # 2 , floored quotient
print(a % b) # 2, remainder of division
print(a ** b) # 248832, a to the power b
print(pow(a, b)) # 248832, a to the power b
print(divmod(a, b)) # (2,2), the pair (a // b, a % b)
NameError: name 'math' is not defined
Just add this line before using the functions
import math
The math module in Python provides access to mathematical functions that are not built into the basic operators. Functions like square root, logarithm, and trigonometric calculations require the math module.
import math
num = 25
# Without math module (will cause an error)
# print(num ** 0.5) # Works but not always accurate
# Using math module
print(math.sqrt(num)) # Output: 5.0
Thus, for precise mathematical calculations, we should import the math module in Python.
bitwise operators | Using bitwise operators |
floor & ceil | Get lower or higher integer from the input number |
fabs | absolute value of input number |
factorial | !n is the factorial of input number n |
copysign | Copy sign of one number to other |
fmod | Module of two input numbers |
frexp | Getting mantissa and exponent of input number |
ldexp | Getting the number by using mantissa and exponent |
fsum() | Sum of elements of iterable object |
isfinite() | checking the number is finite or not |
isinf() | checking the number is infinity or not |
isnan() | checking the number is NaN or not |
modf() | Separating fraction and integer parts |
remainder() | remainder of one number with respect to other |
round() | Rounding number to nearest float or Integer |
trunc() | Integer part of a number |
exp() | e raised to the power x |
expm1() | e raised to the power x -1 |
pow() | x to raised to the power of y |
sqrt() | Returns square root of a number |
log(x[,base]) | Log of x using input base ( optional ) |
log1p(x) | Log of 1+x using base e |
log2(x) | Log of x using base 2 |
log10(x) | Log of x using base 10 |
acos(x) | arc cosine of x ( input in radian ) |
asin(x) | arc sin of x ( input in radian ) |
atan(x) | arc tan of x ( input in radian ) |
cos(x) | cos of x ( input in radian ) |
sin(x) | sin of x ( input in radian ) |
tan(x) | tan of x ( input in radian ) |
acosh(x) | inverse hyperbolic cosine of x |
asinh(x) | inverse hyperbolic sine of x |
atanh(x) | inverse hyperbolic tangent of x |
cosh(x) | hyperbolic cosine of x |
sinh(x) | hyperbolic sine of x |
tanh(x) | hyperbolic tangent of x |
Python's math module provides several essential mathematical constants that help in calculations involving geometry, logarithms, and infinite values.
These constants are useful in various computations, including scientific calculations, probability models, and numerical simulations.
Standard Deviation | Standar Deviation for a list of numbers |
Random Number | Generate Random integer and float |
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.