Mathematical functions in Python with examples

Cecil 210 Published: 06/10/2024

Mathematical functions in Python with examples

I'm sorry! I won't respond in Simplified Chinese this time.

Python is a powerful language that provides various mathematical functions to perform complex calculations. Here are some essential mathematical functions in Python along with their usage and examples:

abs(): Returns the absolute value of a number.

Example: print(abs(-10)) will print 10.

pow(): Calculates the power of a number.

Example: print(pow(2, 3)) will print 8.

max() and min(): Returns the maximum or minimum value from a set of numbers.

Example: print(max(1, 2, 3, 4, 5)) will print 5, and print(min(1, 2, 3, 4, 5)) will print 1.

math.ceil(), math.floor(), and math.trunc(): These functions are from the math module. They return the ceiling (round up), floor (round down), or truncate a number.

Example: import math; print(math.ceil(3.7)) will print 4, and print(math.floor(3.7)) will print 3.

math.sqrt(): Returns the square root of a number.

Example: import math; print(math.sqrt(9)) will print 3.0.

math.exp() and math.log(): These functions return the exponential or natural logarithm of a number, respectively.

Example: import math; print(math.exp(1)) will print 2.718281828459045, and print(math.log(100)) will print 4.605170185958028.

math.sin(), math.cos(), and math.tan(): These functions return the sine, cosine, or tangent of an angle in radians.

Example: import math; print(math.sin(1)) will print a value near 0.8414709848078965, and print(math.cos(3.14)) will print a value near -0.999998857117436.

math.fsum(): Returns the sum of a list of floating-point numbers with the given precision.

Example: import math; print(math.fsum([1, 2, 3])) will print a value close to 6.0.

statistics.mean(), statistics.median(), and statistics.mode(): These functions are from the statistics module and return the mean, median, or mode of a set of numbers.

Example: import statistics; data = [1, 2, 3, 4, 5]; print(statistics.mean(data)) will print 3.0.

numpy (an external library): Offers various mathematical functions, such as linear algebra operations and Fourier transforms. Here's a simple example:

Example: import numpy; data = [1, 2, 3, 4, 5]; print(numpy.mean(data)) will print 3.0.

These are just a few examples of the many mathematical functions available in Python. With these built-in and external libraries, you can perform complex calculations with ease!

Python math sqrt

I'd be happy to help you with that!

Python's Math Sqrt Function

In Python, the math.sqrt() function is a built-in mathematical function that calculates the square root of a given number. This function is part of the Python Standard Library and is available for use in any program.

Here's an example:

import math

num = 4.0

sqrt_num = math.sqrt(num)

print("The square root of", num, "is:", sqrt_num)

Running this code would output: The square root of 4.0 is: 2.0

As you can see, the function correctly calculates the square root of the input number.

Properties of Square Root

Here are some interesting properties and applications of square roots:

Non-Negative Inputs: The math.sqrt() function only works with non-negative inputs. If you try to calculate the square root of a negative number or zero, Python will raise a ValueError. Real Numbers: Square roots can be applied to real numbers only. Complex square roots are not directly supported by this function. Exact Values: For integers and rational numbers with no decimal expansion (like 16 for example), the square root is an exact value, which means it won't change when you round or truncate the number.

Applications of Square Root

Geometry: The square root is crucial in geometry, as it's used to calculate distances, areas, and volumes of various shapes. Statistics: In statistical analysis, the square root is used for standardization (e.g., Z-scoring), calculating confidence intervals, and performing hypothesis tests. Physics: Square roots appear in many physical laws, such as Newtonian mechanics, electromagnetic theory, and quantum mechanics.

Conclusion

In Python, math.sqrt() is an essential function that helps you calculate the square root of a given number with ease. With this function, you can explore various mathematical concepts, from simple geometric shapes to advanced statistical analysis and even physical laws.

I hope this explanation has been helpful!