Python Tutorial

# Mathematical Functions in Python

In Python, there are several built-in mathematical functions available in the math module, which allow you to perform various mathematical operations. To use these functions, you need to import the math module first.These functions can be used to perform a variety of mathematical operations, such as:

1. Arithmetic operations: Addition, subtraction, multiplication, division, and modulus.
2. Trigonometric functions: Sine, cosine, tangent, cotangent, secant, and cosecant.
3. Logarithmic functions: Natural logarithm, logarithm to base 10, and logarithm to any other base.
4. Exponentiation: Exponential function and power function.
5. Rounding functions: Round, ceil, and floor.
6. Random number generation: Random number generation function.

Here are some some commonly used mathematical functions with examples:

## sqrt()

Calculates the square root of a given number.

import math num = 16 square_root = math.sqrt(num) print(square_root) # Output: 4.0

## pow()

Raises a number to a specified power.

import math base = 2 exponent = 3 result = math.pow(base, exponent) print(result) # Output: 8.0

## abs()

Returns the absolute value of a number.

import math num = -5 absolute_value = math.abs(num) print(absolute_value) # Output: 5

## round()

Rounds a floating-point number to the nearest integer or to a specified number of decimal places.

import math num1 = 3.4 rounded_num1 = round(num1) print(rounded_num1) # Output: 3 num2 = 3.7 rounded_num2 = round(num2) print(rounded_num2) # Output: 4 # Specifying the number of decimal places num3 = 3.14159 rounded_num3 = round(num3, 2) print(rounded_num3) # Output: 3.14

## ceil()

Returns the smallest integer greater than or equal to a given number (ceil stands for "ceiling").

import math num = 4.1 ceiled_num = math.ceil(num) print(ceiled_num) # Output: 5

## floor()

Returns the largest integer less than or equal to a given number (floor stands for "floor").

import math num = 4.9 floored_num = math.floor(num) print(floored_num) # Output: 4

## sin(), cos(), tan()

Trigonometric functions that calculate the sine, cosine, and tangent of an angle (in radians).