Python is a powerful programming language with extensive library support, making it an excellent tool for mathematical operations.

In this tutorial, we will walk through a few basic math operations in Python such as arithmetic operations, exponentiation, and using built-in math functions and libraries.

### Step 1: Arithmetic Operations

Python allows us to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulo directly. Here’s how we do it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Addition sum = 3 + 4 print("The sum is:", sum) # Subtraction subtraction = 10 - 5 print("The subtraction is:", subtraction) # Multiplication multiplication = 2 * 3 print("The multiplication is:", multiplication) # Division division = 6 / 2 print("The division is:", division) # Modulo (remainder of division) modulo = 8 % 3 print("The modulo is:", modulo) |

### Step 2: Exponentiation

Python has a built-in operator, `**`

, for exponentiation, which allows us to raise a number to a given power:

1 2 3 |
# Exponentiation exponentiation = 2 ** 3 print("The exponentiation is:", exponentiation) |

Alternatively, we can use the built-in `pow()`

function:

1 2 3 |
# Exponentiation using pow() function exponentiation = pow(2, 3) print("The exponentiation is:", exponentiation) |

### Step 3: Utilizing Built-In Math Functions

Python provides several built-in math functions that can be very useful for complex calculations. These functions include `abs()`

, `round()`

, `max()`

, `min()`

, and many others. Some examples:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Absolute value absolute = abs(-10) print("The absolute value is:", absolute) # Rounding a float rounded = round(3.1415926535, 2) print("The rounded value is:", rounded) # Finding the maximum and minimum values maximum = max(1, 3, 5, 7, 9) minimum = min(1, 3, 5, 7, 9) print("The maximum value is:", maximum) print("The minimum value is:", minimum) |

### Step 4: Using the Math Library

For more advanced mathematical functions, we can use the `math`

library. This library provides a vast collection of functions, including trigonometry, logarithms, combinatorics, and many others. To use the `math`

library, we need to import it first:

1 |
import math |

Now, we can use the various math functions available in this library:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Square root square_root = math.sqrt(16) print("The square root is:", square_root) # Factorial factorial = math.factorial(5) print("The factorial is:", factorial) # Logarithm logarithm = math.log(100, 10) # Using log100 using base 10 print("The logarithm is:", logarithm) # Trigonometry sine = math.sin(math.radians(30)) # We'll need to convert the angle to radians print("The sine of 30 degrees is:", sine) |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import math # Arithmetic Operations sum = 3 + 4 subtraction = 10 - 5 multiplication = 2 * 3 division = 6 / 2 modulo = 8 % 3 # Exponentiation exponentiation = 2 ** 3 exponentiation_pow = pow(2, 3) # Built-In Math Functions absolute = abs(-10) rounded = round(3.1415926535, 2) maximum = max(1, 3, 5, 7, 9) minimum = min(1, 3, 5, 7, 9) # Math Library Functions square_root = math.sqrt(16) factorial = math.factorial(5) logarithm = math.log(100, 10) sine = math.sin(math.radians(30)) # Print results print(f"The sum is: {sum}") print(f"The subtraction is: {subtraction}") print(f"The multiplication is: {multiplication}") print(f"The division is: {division}") print(f"The modulo is: {modulo}") print(f"The exponentiation is: {exponentiation}") print(f"The exponentiation (using pow) is: {exponentiation_pow}") print(f"The absolute value is: {absolute}") print(f"The rounded value is: {rounded}") print(f"The maximum value is: {maximum}") print(f"The minimum value is: {minimum}") print(f"The square root is: {square_root}") print(f"The factorial is: {factorial}") print(f"The logarithm is: {logarithm}") print(f"The sine of 30 degrees is: {sine}") |

The sum is: 7 The subtraction is: 5 The multiplication is: 6 The division is: 3.0 The modulo is: 2 The exponentiation is: 8 The exponentiation (using pow) is: 8 The absolute value is: 10 The rounded value is: 3.14 The maximum value is: 9 The minimum value is: 1 The square root is: 4 The factorial is: 120 The logarithm is: 2.0 The sine of 30 degrees is: 0.5

## Conclusion

Now that we have covered some basic mathematical operations in Python, you should have a good foundation to start building more complex calculations and programs. By leveraging the built-in functions and the `math`

library, you’ll be able to tackle even the most advanced mathematical problems with ease.