Header Ads Widget

Ticker

6/random

Python Math

Python Math


In Python, the `math` module provides a set of mathematical functions and constants. 


Here's an explanation with examples:


1. Basic Mathematical Operations:


```python

import math


result_addition = math.add(3, 5) # Adds two numbers

result_subtraction = math.subtract(8, 3) # Subtracts second number from the first

result_multiplication = math.multiply(4, 6) # Multiplies two numbers

result_division = math.divide(10, 2) # Divides first number by the second


print(result_addition, result_subtraction, result_multiplication, result_division)

```


The `math` module provides basic arithmetic operations.


2. Exponential and Logarithmic Functions:


```python

import math


result_power = math.pow(2, 3) # Raises 2 to the power of 3

result_square_root = math.sqrt(16) # Calculates the square root

result_log = math.log(8, 2) # Calculates the logarithm base 2 of 8


print(result_power, result_square_root, result_log)

```


Functions like `pow`, `sqrt`, and `log` provide operations on powers, square roots, and logarithms.


3. Trigonometric Functions:


```python

import math


angle_rad = math.radians(45) # Converts degrees to radians

result_sin = math.sin(angle_rad) # Calculates the sine of the angle

result_cos = math.cos(angle_rad) # Calculates the cosine of the angle

result_tan = math.tan(angle_rad) # Calculates the tangent of the angle


print(result_sin, result_cos, result_tan)

```


Trigonometric functions such as `sin`, `cos`, and `tan` work with angles in radians.


4. Constants:


```python

import math


pi_value = math.pi # Value of Pi (Ï€)

e_value = math.e # Value of Euler's number (e)


print(pi_value, e_value)

```


The `math` module provides constants like Pi (`pi`) and Euler's number (`e`).


5. Ceiling and Floor Functions:


```python

import math


result_ceiling = math.ceil(4.2) # Rounds up to the nearest integer

result_floor = math.floor(4.9) # Rounds down to the nearest integer


print(result_ceiling, result_floor)

```


The `ceil` function rounds a number up, and the `floor` function rounds it down to the nearest integer.


These are just a few examples of the functionalities provided by the `math` module. It offers a wide range of mathematical operations and constants for various mathematical computations in Python.

Post a Comment

0 Comments