Build a Better Process

An Introduction to Python Functions and Data Types

Objects in Python are given a type. Functions we cover here will help you view and change those types.
  1. Python 3 - Find your version number.
  2. How we got here - See where to access help for your version of Python.
  3. Built-in functions - Review the full list of 68 functions.
  4. Data types - Learn to use the type function and string function.
  5. Next: math - Discuss another data type, integers.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
Remember to practice typing right in your Interpreter to learn faster.

Outline Back Tip Next

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

An ad-free and cookie-free website.

A first look at Python functions and data types


Video Tutorial

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)

Code Examples and Video Script

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)

Step 1 - How to Find the Installed Python Version

Let's jump to the Linux terminal and use the python3 command with the -V option to show the version number, 3.4.2.

paul@fullstack:~$ python3 -V 3.4.2

Step 2 - Find Python Documentation at python.org

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.

Step 3 - Discuss Python Built-in Functions

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.

Documentation for built-in functions on python.org

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.

Documentation for the type() function

(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.

Step 4 - Practice with Python type and Python string functions

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.

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. >>> first_name = "Paul" >>> print("My name is " + first_name) My name is Paul

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.

Using the Python type function

Now what if we want to know the data type of that variable. We can use type(first_name) and Enter.

>>> type(first_name) <class 'str'>

It says 'str', short for string.

Remember, we touched on two other data types: integers and floats. We set y = 2.

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

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.

>>> y 2
Using the Python string function

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.

>>> str(y) '2'

But did we change the type (of the variable)?

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

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?

>>> y = str(y) >>> type(y) <class 'str'> >>> y '2' >>> exit() paul@fullstack:~$

A string, as evidenced by the surrounding apostrophe symbols.

Step 5 - Next: Python Math and Integers

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.

What's Next?

Make sure to connect for reminders because its easy to get distracted.

Outline Back Tip Next

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

introduction to python functions
python type function
python string function
what are python data types
python type
python str
python built-in functions
python print
python version
what is a function in python
python integer
python data types
python tutorial

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