/ factorpad.com / tech / full-stack / python-math-functions.html
An ad-free and cookie-free website.
Beginner
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)
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)
python3
(Functions in Python)
round()
int()
float()
type()
exit()
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
quick python3 -V
to see your version
of Python.
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() |
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.
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.
While this is fresh, let's try it out. Let's assign
2.33
to the
variable x
. Then
round(x)
and
Enter
.
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
type()
function
for x
.
What do you think it will return as the data type? A float or an integer?
An 'integer'
. It rounded to zero
decimal places, making it the integer
2
, and returned the data type using
the type()
function.
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
float of 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
.
So the x
variable still has
2.33
in it, and it's a float.
Let's use our new int()
function and
pass the x
variable to it with
int(x)
.
It returns an integer 2
. We didn't
change x
, we just viewed it as an
integer temporarily.
Now let's assign the integer 2
to
y
, and print
y
,
type(y)
, it's an integer.
We can use the new float()
function
to view the integer y
as a floating
point number, 2.0
.
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.
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 for reminders.
/ factorpad.com / tech / full-stack / python-math-functions.html
A newly-updated free resource. Connect and refer a friend today.