Exploring Python's Built-in Math Functions

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will explore various built-in Python functions for performing mathematical operations. We will start with basic operations such as addition, subtraction, and multiplication, and then move on to more advanced concepts like trigonometry, logarithms, and other mathematical functions. By the end of this lab, you should have a good understanding of how to use these functions in your code.

Achievements

  • Built-in Math Functions
  • Advanced Mathematical Functions
  • Handling Complex Numbers

Built-in Math Functions

Python provides several other built-in mathematical functions that can be useful in different situations.

Open up a new Python interpreter.

python

Here are a few examples:

>>> abs(-3) ## returns the absolute value of a number
3
>>> max(1, 2, 3) ## returns the maximum value of a sequence
3
>>> min(1, 2, 3) ## returns the minimum value of a sequence
1
>>> round(3.14) ## rounds a float to the nearest integer
3
>>> round(3.14, 1) ## rounds a float to a given number of decimal places
3.1
>>> sum([1, 2, 3]) ## returns the sum of a sequence of numbers
6

The abs() function returns the absolute value of a number, max() and min() return the maximum and minimum values of a sequence of numbers, respectively, round() can be used to round a floating-point number to the nearest integer or to a given number of decimal places, and sum() returns the sum of a sequence of numbers.

Advanced Mathematical Functions

Python has a built-in math module that provides more advanced mathematical functions.

Here are a few examples:

import math

>>> math.sqrt(16)
4.0
>>> math.pow(2, 3)
8.0
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
>>> math.sin(math.pi / 2)
1.0
>>> math.cos(math.pi)
-1.0
>>> math.tan(math.pi / 4)
1.0
>>> math.log(10)
2.302585092994046
>>> math.log10(10)
1.0

As you can see, the math module provides functions for square root, exponentiation, common constants like pi and e, trigonometric functions, and logarithms.

Handling Complex Numbers

Python has built-in support for handling complex numbers. A complex number is defined as a + bj where a and b are real numbers and j is the imaginary unit which is defined as sqrt(-1).

Here are some examples of working with complex numbers:

>>> a = 3 + 4j
>>> b = 1 - 2j
>>> a + b
(4+2j)
>>> a - b
(2+6j)
>>> a * b
(-5+2j)
>>> a / b
(-0.2+1.6j)

You can access the real and imaginary parts of a complex number by the real and imag attributes:

>>> a.real
3.0
>>> a.imag
4.0

And you can use the abs() function to get the magnitude of a complex number:

>>> abs(a)
5.0

Summary

In this lab, We looked at more advanced mathematical functions provided by the math module, including square roots, exponentiation, trigonometric functions, logarithms, and common constants like pi and e.

Additionally, We also looked at how to work with complex numbers in Python, including how to add, subtract, multiply, and divide complex numbers, as well as how to access the real and imaginary parts of a complex number, and how to calculate its magnitude.

And we saw some other mathematical functions that python provides such as abs(), max(), min(), round(), sum() which you can use to perform different mathematical operations in your own code.

Keep in mind that Python also provides other libraries like NumPy and SciPy to perform mathematical operations with high precision and efficiency. And feel free to experiment with the different functions and operations that you've learned about in this lab.

Other Python Tutorials you may like