a = 12b = 5print(a + b) # 17 , sum of two numbersprint(a - b) # 7 , subtraction of two numbersprint(a * b) # 60, product of two numbersprint(a / b) # 2.4, quotient of two numbersprint(a // b) # 2 , floored quotientprint(a % b) # 2, remainder of divisionprint(a ** b) # 248832, a to the power bprint(pow(a, b)) # 248832, a to the power bprint(divmod(a, b)) # (2,2), the pair (a // b, a % b)
Importing Math library
You may get error saying about the missing math library need to be included before using these functions.
NameError: name 'math' is not defined
Just add this line before using the functions
import math
Why Import the math Module?
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.
Example: Calculating Square Root
import math
num = 25# Without math module (will cause an error)# print(num ** 0.5) # Works but not always accurate# Using math moduleprint(math.sqrt(num)) # Output: 5.0
Explanation:
Without the math module: We can calculate square roots using **0.5, but it may not be precise for large or complex numbers.
Using math.sqrt(): This function ensures better accuracy and handles special cases correctly.
Thus, for precise mathematical calculations, we should import the math module in Python.