/ factorpad.com / tech / full-stack / python-functions.html
An ad-free and cookie-free website.
Beginner
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
An introduction to Python functions and data types | Python for Beginners (4:39)
Welcome. Today's question: What are Python functions?
I'm Paul, and in the last video we write our first program using
print()
, which is a function.
Most instructors just throw functions at students without explaining what they are and how to find documentation.
So here I'll take a different approach, showing you how to find your version of Python 3, then where to access its documentation.
We'll start easy, with what are called built-in functions. Then we'll use a few in the Python Interpreter related to data types, before moving on to math functions in the next video on our journey.
(Commands in Linux)
python3
(Functions in Python)
type()
str()
print()
exit()
Let's jump to the Linux terminal and use the
python3
command with the
-V
option to show the version number,
3.4.2.
I will tab over to a browser to show you something interesting.
At this location, https://www.python.org/doc/versions/, which I will include in the (YouTube) Description, you will find documentation for each historical release, roughly once-per-quarter.
My version here is somewhat dated, from (4 October) 2014.
You may be asking: why don't I upgrade? Well, if you've been with us, I'm using Debian version 8, and Python 3.4.2 has been fully tested by the Linux distribution provider Debian. It was the default version installed, in tutorial 22, so I've stuck with it.
Later, I'll be more adventurous, but for now let's focus on 3.4.2 documentation.
I want to draw your attention to built-ins, and yes, they're similar to Linux bash shell builtins covered in tutorial 15.
And built-in means we don't have to load another module, because modules I'm saving for later.
From https://www.python.org/doc/versions/ I'll click on 3.4.2 and then Python Library Reference, and Built-In Functions, and there they are, the 68 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() |
complex() | hasattr() | max() | round() | |
delattr() | hash() | memoryview() | set() |
You can see print()
from the last
video, type()
and
str()
for strings.
(Click on type(), taking you to the page: https://docs.python.org/release/3.4.2/library/functions.html#type)
Let's try out the documentation for
type()
and it's not beginner friendly,
but I want you to know it's there. You can read it later. Basically
there are two ways to use type()
, and
we'll use the first one, with one argument, where it returns the
type of an object.
Let's go back to what we did in the last tutorial. Remember we had a
two-liner like this, we set up a variable called
first_name
and assigned
Paul
to it in quotes.
We could have enclosed it in apostrophe symbols
''
just as easily. Either
way works, but head back there if you don't remember why we have two
ways to input text.
In the second line we used a function, and functions go like this,
the function name, open parenthesis (
,
then pass it objects or arguments, closed parenthesis
)
and
Enter
.
This one put together, or concatenated, with the plus symbol
+
, two text strings, with the second
being that variable first_name
. Okay,
good.
Now what if we want to know the data type of that variable. We can use
type(first_name)
and
Enter
.
It says 'str'
, short for string.
Remember, we touched on two other data types: integers and floats. We
set y = 2
.
Let's use a type()
on
y
.
Notice how Python automatically interpreted the
2
as an
'int'
, or integer.
Viewing y
and it returns
2
. If it were surrounded by
apostrophes, it'd be a string.
With the str()
function we can view
that integer stored in y
, as a string,
if we needed to for a program, as an example.
But did we change the type (of the variable)?
Using type(y)
again, and no, it's
still an integer. We just viewed it as a string.
If we wanted to change y
to a string,
we could reassign the old y
with
y = str(y)
and now what is the data
type for y
?
A string, as evidenced by the surrounding apostrophe symbols.
Very good. So integers I'm saving for the next video on our journey to Data Science, following this path.
You are welcome to join, just subscribe to be notified of future videos.
Have a nice day.
Make sure to connect for reminders because its easy to get distracted.
/ factorpad.com / tech / full-stack / python-functions.html
A newly-updated free resource. Connect and refer a friend today.