---
title: "Explain the Python Math with example."  
description: "Explain the Python Math with example."  
author: "Gulab"  
published: 2025-10-08  
updated: 2025-10-08  
canonical: https://www.mindstick.com/interview/34388/explain-the-python-math-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 6 minutes  

---

# Explain the Python Math with example.

## 1. Basic Arithmetic in Python

Python can perform basic mathematical operations directly using arithmetic operators:

| Operator | Description | Example | Output |
| --- | --- | --- | --- |
| `+` | Addition | `5 + 3` | `8` |
| `-` | Subtraction | `9 - 4` | `5` |
| `*` | Multiplication | `7 * 6` | `42` |
| `/` | Division (float) | `8 / 2` | `4.0` |
| `//` | Floor Division | `7 // 2` | `3` |
| `%` | Modulus (remainder) | `10 % 3` | `1` |
| `**` | Exponentiation | `2 ** 3` | `8` |

## 2. Math with the `math` Module

Python has a built-in `math` module for more advanced mathematical functions.

You first need to import it:

```python
import math
```

Here are the main features:

### Common Math Functions

| Function | Description | Example | Output |
| --- | --- | --- | --- |
| `math.sqrt(x)` | Square root | `math.sqrt(16)` | `4.0` |
| `math.pow(x, y)` | Power | `math.pow(2, 3)` | `8.0` |
| `math.ceil(x)` | Round up | `math.ceil(3.2)` | `4` |
| `math.floor(x)` | Round down | `math.floor(3.8)` | `3` |
| `math.fabs(x)` | Absolute value | `math.fabs(-5.6)` | `5.6` |
| `math.factorial(x)` | Factorial | `math.factorial(5)` | `120` |
| `math.fmod(x, y)` | Modulus | `math.fmod(10, 3)` | `1.0` |

## 3. Trigonometric Functions

The `math` module supports trigonometry (angles in **radians**, not degrees).

| Function | Description | Example |
| --- | --- | --- |
| `math.sin(x)` | Sine | `math.sin(math.pi/2)` → `1.0` |
| `math.cos(x)` | Cosine | `math.cos(0)` → `1.0` |
| `math.tan(x)` | Tangent | `math.tan(math.pi/4)` → `1.0` |
| `math.asin(x)` | Inverse sine | `math.asin(1)` → `1.5708` |
| `math.degrees(x)` | Convert radians → degrees | `math.degrees(math.pi)` → `180.0` |
| `math.radians(x)` | Convert degrees → radians | `math.radians(180)` → `3.14159` |

## 4. Logarithmic and Exponential Functions

| Function | Description | Example | Output |
| --- | --- | --- | --- |
| `math.exp(x)` | e raised to power x | `math.exp(2)` | `7.389` |
| `math.log(x)` | Natural log (base e) | `math.log(10)` | `2.302` |
| `math.log10(x)` | Log base 10 | `math.log10(1000)` | `3.0` |
| `math.log2(x)` | Log base 2 | `math.log2(8)` | `3.0` |

## 5. Constants

The `math` module provides useful constants:

| Constant | Description | Example Output |
| --- | --- | --- |
| `math.pi` | π (3.14159...) | `3.141592653589793` |
| `math.e` | e (2.71828...) | `2.718281828459045` |
| `math.inf` | Infinity | `math.inf` |
| `math.nan` | Not-a-Number | `math.nan` |

## 6. Rounding and Comparison

| Function | Description | Example |
| --- | --- | --- |
| `round(x, n)` | Round to *n* decimal places | `round(3.14159, 2)` → `3.14` |
| `math.isfinite(x)` | Check if finite | `math.isfinite(100)` → `True` |
| `math.isnan(x)` | Check if NaN | `math.isnan(float('nan'))` → `True` |

## Example Program

```python
import math

radius = 5
area = math.pi * math.pow(radius, 2)
print("Area of circle:", area)

angle_deg = 45
angle_rad = math.radians(angle_deg)
print("Sine of 45°:", math.sin(angle_rad))

print("Log base 10 of 1000:", math.log10(1000))
print("Ceiling of 3.14:", math.ceil(3.14))
```

## Output:

```plaintext
Area of circle: 78.53981633974483
Sine of 45°: 0.7071067811865475
Log base 10 of 1000: 3.0
Ceiling of 3.14: 4
```

## Answers

### Answer by Gulab

## 1. Basic Arithmetic in Python

Python can perform basic mathematical operations directly using arithmetic operators:

| Operator | Description | Example | Output |
| --- | --- | --- | --- |
| `+` | Addition | `5 + 3` | `8` |
| `-` | Subtraction | `9 - 4` | `5` |
| `*` | Multiplication | `7 * 6` | `42` |
| `/` | Division (float) | `8 / 2` | `4.0` |
| `//` | Floor Division | `7 // 2` | `3` |
| `%` | Modulus (remainder) | `10 % 3` | `1` |
| `**` | Exponentiation | `2 ** 3` | `8` |

## 2. Math with the `math` Module

Python has a built-in `math` module for more advanced mathematical functions.

You first need to import it:

```python
import math
```

Here are the main features:

### Common Math Functions

| Function | Description | Example | Output |
| --- | --- | --- | --- |
| `math.sqrt(x)` | Square root | `math.sqrt(16)` | `4.0` |
| `math.pow(x, y)` | Power | `math.pow(2, 3)` | `8.0` |
| `math.ceil(x)` | Round up | `math.ceil(3.2)` | `4` |
| `math.floor(x)` | Round down | `math.floor(3.8)` | `3` |
| `math.fabs(x)` | Absolute value | `math.fabs(-5.6)` | `5.6` |
| `math.factorial(x)` | Factorial | `math.factorial(5)` | `120` |
| `math.fmod(x, y)` | Modulus | `math.fmod(10, 3)` | `1.0` |

## 3. Trigonometric Functions

The `math` module supports trigonometry (angles in **radians**, not degrees).

| Function | Description | Example |
| --- | --- | --- |
| `math.sin(x)` | Sine | `math.sin(math.pi/2)` → `1.0` |
| `math.cos(x)` | Cosine | `math.cos(0)` → `1.0` |
| `math.tan(x)` | Tangent | `math.tan(math.pi/4)` → `1.0` |
| `math.asin(x)` | Inverse sine | `math.asin(1)` → `1.5708` |
| `math.degrees(x)` | Convert radians → degrees | `math.degrees(math.pi)` → `180.0` |
| `math.radians(x)` | Convert degrees → radians | `math.radians(180)` → `3.14159` |

## 4. Logarithmic and Exponential Functions

| Function | Description | Example | Output |
| --- | --- | --- | --- |
| `math.exp(x)` | e raised to power x | `math.exp(2)` | `7.389` |
| `math.log(x)` | Natural log (base e) | `math.log(10)` | `2.302` |
| `math.log10(x)` | Log base 10 | `math.log10(1000)` | `3.0` |
| `math.log2(x)` | Log base 2 | `math.log2(8)` | `3.0` |

## 5. Constants

The `math` module provides useful constants:

| Constant | Description | Example Output |
| --- | --- | --- |
| `math.pi` | π (3.14159...) | `3.141592653589793` |
| `math.e` | e (2.71828...) | `2.718281828459045` |
| `math.inf` | Infinity | `math.inf` |
| `math.nan` | Not-a-Number | `math.nan` |

## 6. Rounding and Comparison

| Function | Description | Example |
| --- | --- | --- |
| `round(x, n)` | Round to *n* decimal places | `round(3.14159, 2)` → `3.14` |
| `math.isfinite(x)` | Check if finite | `math.isfinite(100)` → `True` |
| `math.isnan(x)` | Check if NaN | `math.isnan(float('nan'))` → `True` |

## Example Program

```python
import math

radius = 5
area = math.pi * math.pow(radius, 2)
print("Area of circle:", area)

angle_deg = 45
angle_rad = math.radians(angle_deg)
print("Sine of 45°:", math.sin(angle_rad))

print("Log base 10 of 1000:", math.log10(1000))
print("Ceiling of 3.14:", math.ceil(3.14))
```

## Output:

```plaintext
Area of circle: 78.53981633974483
Sine of 45°: 0.7071067811865475
Log base 10 of 1000: 3.0
Ceiling of 3.14: 4
```


---

Original Source: https://www.mindstick.com/interview/34388/explain-the-python-math-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
