Python Math Functions

Python provides several built-in mathematical functions for performing calculations. You can also use the math module for advanced mathematical operations. abs() The abs() function returns the absolute (positive) value of a number. Syntax Example Output: pow() The pow() function returns the value of a number raised to a specified power. Syntax Example Output: min() The […]

2 mins read Lesson 32 of 56 57% through track

Python provides several built-in mathematical functions for performing calculations. You can also use the math module for advanced mathematical operations.

abs()

The abs() function returns the absolute (positive) value of a number.

Syntax

abs(number)

Example

print(abs(-25))

Output:

25

pow()

The pow() function returns the value of a number raised to a specified power.

Syntax

pow(x, y)

Example

print(pow(2, 3))

Output:

8

min()

The min() function returns the smallest value.

Example

print(min(10, 5, 20, 3))

Output:

3

max()

The max() function returns the largest value.

Example

print(max(10, 5, 20, 3))

Output:

20

round()

The round() function rounds a number to the nearest integer.

Example

print(round(5.7))
print(round(5.3))

Output:

6
5

math.sqrt()

The sqrt() function returns the square root of a number.

Example

import math

print(math.sqrt(64))

Output:

8.0

math.ceil()

The ceil() function rounds a number upward to the nearest integer.

Example

import math

print(math.ceil(4.2))

Output:

5

math.floor()

The floor() function rounds a number downward to the nearest integer.

Example

import math

print(math.floor(4.8))

Output:

4

math.pi

The pi constant returns the value of π (pi).

Example

import math

print(math.pi)

Output:

3.141592653589793

math.factorial()

The factorial() function returns the factorial of a number.

Example

import math

print(math.factorial(5))

Output:

120

Summary

  • abs() returns the absolute value.
  • pow() raises a number to a power.
  • min() returns the smallest value.
  • max() returns the largest value.
  • round() rounds a number to the nearest integer.
  • math.sqrt() calculates the square root.
  • math.ceil() rounds upward.
  • math.floor() rounds downward.
  • math.pi provides the value of pi.
  • math.factorial() calculates factorials.