Faster Learning Tutorials

# Eight Python builtin math functions

Here we introduce 5 new math functions are built-in to Python and review 3 we learned earlier.
1. Built-in functions - Review a list of 68 functions that come with Python 3.
2. Review - See three basic math functions covered.
3. Single-number - Introduce two new functions that operate on single numbers.
4. Multi-number - Try out three functions that operate on ranges of numbers.
5. Next: new project! - See where we head next.
###### Paul Alan Davis, April 8, 2017
Updated: November 1, 2018
##### Here we start with native math functions that need not be imported before tackling advanced math functions later.

~/ home  / tech  / full stack  / python math builtins

## Learn 8 built-in Python math functions

Beginner

### Video Tutorial

Eight Python builtin math functions | Python for Beginners (4:51)

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

### Code Examples and Video Script

Welcome. Today's question: Which 8 math functions should Python beginners learn first?

I'm Paul, and here we cover data science, so it's important that we learn basic math functions before heading off to the exotic, fun and head-scratching stuff like Statistics.

So here we'll stick with functions built-in to Python with a review of 3 we covered earlier, then tack on 2 for calculations on single numbers, followed by 3 that operate on a range of numbers.

This closes out the 11-video Project 3 (Python for Beginners). Hang on for the end and I'll tell you what Project 4 is all about.

(Commands in Linux)

• `less`
• `python3`

(Functions in Python)

• `abs()`
• `pow()`
• `max()`
• `min()`
• `sum()`
• `round()`
• `int()`
• `float()`

#### Step 1 - Reference a list of 68 Python Built-in Functions

Let's head to the Linux terminal and here's a file with all of the built-in functions in this version of Python.

paul@fullstack:~\$ less notes/python_builtins.txt

Noted is where each function was introduced, and here in video 32, we're checking off absolute value, power, maximum, minimum and summation.

Built-in Functions
abs() (32) dict() help(28) min() (32) setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() (27) open() str() (26)
bool() exec() isinstance() ord() (26) sum() (32)
bytearray() filter() issubclass() pow() (32) super()
bytes() float() (27) iter() print() (26) tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() (32) round() (27)
delattr() hash() memoryview() set()
##### Official Python documentation at python.org

(Also See official documentation at the official python.org.)

##### Review the Python type function, integers and decimals

For now we're working with two data types in math, we have integers, and when entered without a decimal place, Python assumes it's an integer.

paul@fullstack:~\$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = -2 >>> type(x) <class 'int'>

We also have floats, or floating point numbers entered with a decimal.

>>> y = -2.0 >>> type(y) <class 'float'>

So with 8 functions to cover; let's get cracking.

#### Step 2 - Review Python int, round and float

##### Using the Python int function

Function 1, `int()`, views a float as an integer.

>>> y -2.0 >>> int(y) -2 >>> type(y) <class 'float'>

It won't change it; it will just view it (as an integer).

##### Using the Python float function

Function 2, `float()`, does the opposite, view an integer as a float.

>>> x -2 >>> float(x) -2.0
##### Using the Python round function

Function 3, `round()`, and here we input how many decimal places we'd like to round a number to, using an optional argument after a comma, telling Python where you want it to round.

>>> round(y, 1) -2.0 >>> round(y) -2 >>> help(round)

The default rounds to a whole number. And there's object help, if you forget how it works.

Help on built-in function round in module builtins: round(...) round(number[, ndigits]) -> number Round a number to a given precision in decimal digits (default 0 digits). This returns an int when called with one argument, otherwise the same type as the number. ndigits may be negative. (END)

#### Step 3 - Introduce Python abs, power, max and min

##### Using the Python abs function

Function 4, is absolute value. Here's the help.

>>> help(abs)

And `q` to leave.

Help on built-in function abs in module builtins: abs(...) abs(number) -> number Returns the absolute value of the argument. (END)

Here we input one number and it returns the absolute value, so `abs(x)` which is negative 2, becomes positive 2.

>>> x -2 >>> abs(x) 2 >>> y -2.0 >>> abs(y) 2.0

It works with floats too.

##### Using the Python pow function

Function 5, Python power. Let's get in the habit of looking at help, `help(pow)` in this case.

>>> help(pow)

And let's do the two argument version.

Help on built-in function pow in module builtins: pow(...) pow(x, y[, z]) -> number With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) %z, but may be more efficient (e.g. for ints). (END)

So it goes 3 to the power of 2 is 9, and going the other way, 9 to the power of one-half, or the square root, is 3.0.

>>> pow(3, 2) 9 >>> pow(9, 1/2) 3.0

#### Step 4 - Introduce Python max, min and sum

##### Using the Python max function

For function 6, let's set up a range of numbers, let's call them grades, and assign some percentile scores over a semester, like this.

I'll put them in what is called a list like this, and use `max()` to find the highest grade of 99.

>>> grades = [79, 85, 99, 92, 87] >>> max(grades) 99 >>> help(max)

Using `help()` we can see there are two ways to work with `max()`, and here we're using the second option, saving the first on iterables for later.

Help on built-in function max in module builtins: max(...) pow(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument. (END)
##### Using the Python min function

Function 7, `min()` is done the same way.

>>> min(grades) 79

No surprises there.

##### Using the Python sum function

Function 8, summation or `sum()`. Here's how that works, and it won't work on text, which is obvious.

Let's throw the list of grades in and we get 442.

>>> sum(grades) 442 >>> sum(grades) / 5 88.4 >>> exit()

But that's more meaningful if we get an average, dividing by 5 and that gives us 88.4, eh, a B-plus.

On the topic of average, and other statistical calculations we won't have to put them together ourselves like this. Instead, in later videos we'll import modules allowing us to do linear algebra, regressions and all sorts of math with neat visualizations.

Also, if you're not familiar with my background, in a previous life, I managed \$5 Billion in stock mutual funds in the US, so the subject matter expertise I'll bring to the table is in the financial markets.

So a number of projects will be geared to those aspiring to be financial engineers, quants and portfolio managers. We'll also use Python to analyze data for marketing, social media and more.

#### Step 5 - Next: New Project!

In the meantime, this concludes Project 3, and in Project 4 we'll kick off HTML and CSS for Beginners so we can create web pages to view all of our neat output in a browser.

And coding right in the Python Interpreter will be phased out as well. We'll work with multi-line files almost exclusively.

• Client : HTML, CSS, JavaScript
• Software : Python Scientific Stack
• Data : PostgreSQL, MySQL
• OS : Linux (command line), Debian

To be part of the fun, please subscribe and comment so others can learn from your insights, and your observations.

Have a nice day.

## What's Next?

Check out other learning content at our YouTube Channel and subscribe here.

• To access all tutorials, click Outline.
• To learn about Python escape sequences for printing, click Back.
• While we create new content, Next is disabled.

~/ home  / tech  / full stack  / python math builtins

Keywords:
python functions
python math functions
python abs function
python absolute value
python max
python maximum
python min
python minimum
python pow
python power
python sum
python int
python integer
python float
python round
python builtin functions
python builtin math functions
python tutorial