An introduction to Python functions and data types | Python for Beginners (4:39)
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
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)
(Functions in Python)
Let's jump to the Linux terminal and use the
python3 command with the
-V option to show the version number,
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.
You can see
print() from the last
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
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
This one put together, or concatenated, with the plus symbol
+, two text strings, with the second
being that variable
Now what if we want to know the data type of that variable. We can use
'str', short for string.
Remember, we touched on two other data types: integers and floats. We
y = 2.
Let's use a
Notice how Python automatically interpreted the
2 as an
'int', or integer.
y and it returns
2. If it were surrounded by
apostrophes, it'd be a string.
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)?
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 = str(y) and now what is the data
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.
There's a lot to learn at our YouTube Channel. Subscribe here.