Python Math Functions and Math Module

Python Math Functions and Math Module

Kishore V


Python Math Functions and Math Module

Python provides several built-in mathematical functions for basic calculations. For advanced mathematical operations, Python also includes a powerful math module.

Built-in Math Functions

Finding Minimum and Maximum Values

The min() and max() functions help you quickly determine the smallest and largest values from a collection of numbers.

Example: Find the minimum and maximum values from a set of numbers.

Output:

Minimum value: 7
Maximum value: 89
Try it Yourself

Absolute Value

The abs() function converts a negative number into its positive form, while positive numbers remain unchanged.

Example: Get the absolute value of a number.

Output:

Absolute value: 15.8
Try it Yourself

Power Function

The pow() function raises a number to a specified power.

Example: Calculate 3 raised to the power of 4.

Output:

Power result: 81
Try it Yourself

Python Math Module

For more complex mathematical calculations, Python provides the math module. To access its features, you must import it first.

Syntax: import math

Square Root Calculation

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

Output:

Square root: 9.0
Try it Yourself

Rounding Numbers

    • math.ceil() rounds a number up to the nearest integer.

    • math.floor() rounds a number down to the nearest integer.

Output:

Ceil value: 7
Floor value: 6
Try it Yourself

Mathematical Constant π (Pi)

The math.pi constant provides an accurate value of π (pi), commonly used in geometry and trigonometry.

Output:

Area of circle: 78.53981633974483
Try it Yourself

Our website uses cookies to enhance your experience. Learn More
Accept !