FactorPad
Build a Better Process

Python Math Functions Including round, int and float

The first few math functions are built-in to Python. Later we will add additional modules providing more complex mathematical calculations.
  1. Built-in functions - Review a list of 68 functions that come with Python 3.
  2. Data types - Review data types associated with math.
  3. Math functions - Run through examples of Python round, int and float.
  4. Compare to Excel - See Python/Excel similarities in math functions.
  5. Next: finding help - Learn ways to find help inside Python itself.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
Remember, you can always return here for a refresher.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-math-functions.html


An ad-free and cookie-free website.


Python functions and data types for mathematical operations

Beginner

Video Tutorial

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

Python math functions including round, int and float | Python for Beginners (4:51)

Code Examples and Video Script

Welcome. Today's question: Are functions in Python similar to Excel?

I'm Paul, and I help the self-motivated build their Python skills from what they already know about Excel, because the transition to programming can be frustratingly slow.

So here I'll make a helpful connection to speed things up. We'll focus on simple math, sticking with Python built-ins, review our data types, pick 3 new math functions and discuss what we already know about functions in a spreadsheet.

(Commands in Linux)

(Functions in Python)

Next on our journey, we will learn how to find help in Python.

Step 1 - Find 68 Python Built-in Functions on python.org

Let's jump to the Linux terminal to show you our progress on Python's built-in functions and how to find documentation. You can always do a quick python3 -V to see your version of Python.

paul@fullstack:~$ python3 -V 3.4.2

From the last tutorial, I showed you a website with documentation for all versions, which I will provide a link to.

Mine is 3.4.2 and then (click) Library Reference, Built-in Functions, and there they are, 68 of them, that came pre-loaded with Python.

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

Step 2 - Review Python Data Types

Let's focus on three easy math functions. We already played with floating point numbers (float()) and integers (int()), in tutorials 23 and 24, and in tutorial 26, we used type() to view data types.

Step 3 - Practice with Python round, int and float

We'll use int() and float() here, and a new one, round() to return the floating point value number rounded to ndigits.

(Click on round(), taking you to the page: https://docs.python.org/release/3.4.2/library/functions.html#round)

Brackets in round(number[, ndigits]) indicate that it's optional, meaning it defaults to zero decimal places.

Using the Python round function

While this is fresh, let's try it out. Let's assign 2.33 to the variable x. Then round(x) and Enter.

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.33 >>> round(x) 2

By default, it rounds to zero decimal places.

Good, now let's use the ndigits, with , 1 decimal place.

>>> round(x, 1) 2.3

Okay, and go with me on this one, let's do a round() inside of a type() function for x.

What do you think it will return as the data type? A float or an integer?

>>> type(round(x)) <class 'int'>

An 'integer'. It rounded to zero decimal places, making it the integer 2, and returned the data type using the type() function.

>>> type(round(x, 1)) <class 'float'>

And the answer to the second one, is easy, a float.

Division in Python 3 is different from Python 2

As an aside, don't forget that in Python 3, any division, turns data into a float. To jog your memory, we did 6.0 float, divided by 2 integer and it returned a float of 3.0.

>>> 6.0 / 2 3.0 >>> 6 / 2 3.0

And integer 6 divided by integer 2 returns a float of 3.0.

However, other operators like multiplication of integer 6 times integer 2 returns an integer of 12.

>>> 6 * 2 12
Using the Python int function

So the x variable still has 2.33 in it, and it's a float.

>>> x 2.33 >>> type(x) <class 'float'>

Let's use our new int() function and pass the x variable to it with int(x).

>>> int(x) 2 >>> x 2.33

It returns an integer 2. We didn't change x, we just viewed it as an integer temporarily.

Using the Python float function

Now let's assign the integer 2 to y, and print y, type(y), it's an integer.

>>> y = 2 >>> y 2 >>> type(y) <class 'int'>

We can use the new float() function to view the integer y as a floating point number, 2.0.

>>> float(y) 2.0

Step 4 - Compare Python to Excel

Let's compare Python to Excel. In our first example, we used round() with this format. We did round of x without the optional ndigits argument.

x Python function Python answer Excel formula Excel answer
2.33 round(x) 2 =ROUND(h6,0) 2
2.33 round(x, 1) 2.3 =ROUND(h7,1) 2.3
2.33 type(x) float    
2.33 int(x) 2    
2.33 float(x) 2.33    

(For Excel examples, I suggest watching the video).

It returned integer 2.

Let's try it in Excel, =ROUND and look, there's a similar function in Excel, and with open parenthesis, if you can see it. It provides the format, (=ROUND(number, num_digits)) and it's similar to Python, except num_digits it is required here.

For the second one, we'll round it to 1 decimal place.

Finish the table for homework

For homework, if you have Excel, see if you can find similar functions for 3, 4 and 5. And leave a comment (in YouTube) to help others out.

Okay, so why Excel? Well many people interested in Data Science have analyzed data in a spreadsheet before, especially those in Finance, and it'd be silly to waste all of that knowledge.

Step 5 - Next: Find Help in Python

This is just one Task, number 27, sitting within this one Project (Python for Beginners) on our path to learning Data Science using this software stack.

You are welcome to join, just subscribe to get future videos, like the next one on finding help in Python.

Have a nice day.


What's Next?

We have hundreds of videos available for free learning on our YouTube Channel. Check it out and subscribe for reminders.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-math-functions.html


python functions
python math functions
python round function
python int function
python float function
python built-in functions
python compared to excel
python excel similarities
python integer
python data types
python division
python division float
python tutorial

A newly-updated free resource. Connect and refer a friend today.