Python built-in functions
Nov 21, 2023
DataSciencePursuit
In this section, we will cover built-in functions. These are functions already defined within Python.
From the previous section, we learned that:
- functions are reusable blocks of code that perform a particular task.
- defining a function means creating it while calling a function means using or running that function.
All functions need to be defined first before they are called. Since built-in functions are already defined, we just need to learn how to call them. We will cover how to call and pass arguments to built-in functions. We will also cover how to store the results in variables and then lastly we will learn about methods. Without further ado, let's learn how to call functions.
Calling a function
To call a function, you can use either of the following syntax:
either: function_name()
That is, write the function's name and then parenthesis, if the function does not take any arguments.
or: function_name(arg1,arg2,...)
That is, if the function takes arguments, you'll have different arguments (arg) within the parenthesis:
Arguments
Arguments are values/data that a function can take. In other words, arguments are inputs you can give/pass to a function.
Some functions don't take any arguments, some take only a specific number of arguments, while others take as many arguments as you can give them. When there are multiple arguments, each argument should also be separated by a comma as shown in the syntax above. However, you do not need to memorize specific details for each function as most functions are well documented (you can just google away).
Think about our dishwasher example from the previous video, the soap, dishes and even the water connected to it can be considered arguments. So if we did have a dishwasher function in Python, calling it would look something like this:
dishwasher(soap,dish1,dish2,dish3)
This will give us an error though because it is not a real function, but let's take a look at some real examples.
Built-in function examples
We will start with the print function, which is probably the most used function out of all of them.
Print function
When doing most tutorials, the print function is the first code that people usually see. The print function displays its arguments to the screen. If this sounds familiar, this is similar to the way that our cells work in Jupyter. However, Jupyter only displays the last output in a cell. This is why we have been writing most code in separate cells, but now we can use the print function to display all other output.
let's see this in action, say we had 1, 2, 3
1
2
3
3
We just see the final output, 3, we do not see 1 and 2 here.
Now let's use the print function:
print(1)
print(2)
3
1 2
3
We just left the last one (3) as is because Jupyter takes care of that.
The print function is one of those crazy functions that takes any number of arguments including no arguments.
# No arguments: displays a blank line
print()
This does not seem exciting, but you can use this to create space between other print outputs for readability, especially if you have a lot of output.
# Multiple arguments: prints them in 1 line with a space in between
print(1,2,3)
1 2 3
The print function can be useful for debugging long code in one cell, especially for checking semantic errors, because you can print any output you want. It can also be used for information, for example in our rock paper scissors game, the print function can be used to display game stats to whoever is playing the game.
Another really useful function is the max function
Max function
The max function gives us or in technical terms, returns the largest argument out of the arguments given.
This one requires at least 2 arguments, what is the point of getting the max of one argument right?
max(1,2,90)
90
There is a min function that also works the same way.
Those should be enough examples to get a gist of how functions are called. Well learn more built-in functions in later sections, we will use them a lot.
Now let's look at how to store the outputs that functions return.
Variable assignment
Functions always return a value/data and that value/data can be stored in a variable to be used elsewhere.
Now let's see an example, we will name our variable max_value, and then we will store the max of 2 and 4.
max_value = max(2,4)
Python will first evaluate the max function, then store what the function returns which in this case is 4.
In the next cell, et us see if max_value has the value 4.
max_value
4
Success!
The lines of code could have been in 1 cell, but there is something about the print function you should see.
Let's assign the print function to a variable named output.
output = print(5)
5
The print function does its thing, it displays 5 to the screen. Now let's see what's in output.
output
It looks like there is nothing in there. Although the print function displays its input to the
screen, this is not what it returns to us. It puts that value on the screen and seems to give us
nothing back. It only seems to give us nothing back because it actually returns a value called
None
which represents nothingness to Python.
To see this, you need to tell Python to print that to the screen.
print(output)
None
There are other functions that also return None. We will learn more about what 'return' means when we create functions in the next video.
Important note on naming variables
Notice that we did not name the variables the same as the functions, that is because that would overwrite the functions.
This also applies to any other already reserved names in Python. One way you can easily tell without knowing all reserved names is a color and/or style change when you type the name, variables should just be black and unbolded. Reserved names are different colors and some are bolded as well, for example, functions are green here.
We mentioned it here instead of when we learned about variables because it would likely be easier to remember if you saw an example.
So let's see as an example:
# Overwriting the print function
print = 5
print
5
print(10)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[13], line 1 ----> 1 print(10) TypeError: 'int' object is not callable
To get back to normal functionality, restart the kernel (and make sure to change the variable name). After restarting the kernel, the print function works again:
print(10)
10
Functions as Arguments
Since functions return values, we can also pass functions as arguments to other functions.
For example, we can print the max of 10 and 5. Python first evaluates the max and then prints it.
print(max(10, 5))
10
We can also have the max of max and let's do min this time:
max(max(10, 100),min(40,50),5)
# max(100,40,5) # intermediary step
100
Python first evaluates the functions that are arguments then it evaluates the outer function.
Now let's look at a special subset of functions called methods.
Methods
Methods are functions that belong to/are specific to a particular data type/structure.
These are called a little differently. The syntax is as follows:
data.method_name(arg1,arg2,...)
That is data, dot (.), and then the rest looks like a function call (we have the method name, and parenthesis, with arguments if needed).
Let's see a numeric example using the as_integer_ratio() method which returns a pair of numbers that can be divided to give you the value you provided:
1.5.as_integer_ratio()
(3, 2)
3 divided by 2 gives us 1.5.
We will see a lot of useful methods when we learn about strings and lists.
Next steps
Next, we will write out a plan for our rock paper scissors game. Now that we know how to call built-in functions, we are almost ready to start building out our game.