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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/variables_data_types -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/numeric_types -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/lists -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/tuples -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/importing_modules -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/standard_libraries -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/math_random -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/python_shell -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} python/build_in_functions -.-> lab-87{{"`Exploring Python's Built-in Math Functions`"}} end

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