Python math functions including round, int and float | Python for Beginners (4:51)
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
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.
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
python3 -V to see your version
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.
Let's focus on three easy math functions. We already played with
floating point numbers (
int()), in tutorials 23 and
24, and in tutorial 26, we used
type() to view data types.
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)
indicate that it's optional, meaning it defaults to zero decimal places.
While this is fresh, let's try it out. Let's assign
2.33 to the
By default, it rounds to zero decimal places.
Good, now let's use the ndigits, with
, 1 decimal place.
Okay, and go with me on this one, let's do a
round() inside of a
What do you think it will return as the data type? A float or an integer?
'integer'. It rounded to zero
decimal places, making it the integer
2, and returned the data type using
And the answer to the second one, is easy, a float.
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
6 divided by integer
2 returns a float of
However, other operators like multiplication of integer
6 times integer
2 returns an integer of
x variable still has
2.33 in it, and it's a float.
Let's use our new
int() function and
x variable to it with
It returns an integer
2. We didn't
x, we just viewed it as an
Now let's assign the integer
y, and print
type(y), it's an integer.
We can use the new
to view the integer
y as a floating
Let's compare Python to Excel. In our first example, we used
round() with this format. We did
x without the optional
|x||Python function||Python answer||Excel formula||Excel answer|
(For Excel examples, I suggest watching the video).
It returned integer 2.
Let's try it in Excel,
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
it is required here.
For the second one, we'll round it to 1 decimal place.
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.
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.
We have hundreds of videos available for free learning on our YouTube Channel. Check it out and subscribe here.