This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Reading function documentation

Now, one important skill you should have is to understand how to read Python documentations.

So let us apply what you have learnt about functions to read the documentation for a function, so that you can really understand how to use them.

The official documentation gives a complete list of Python built-in functions, and also their documentation.

The official documentation gives a complete list of built-in functions, and also their documentation.

Let us look at a function that we have already used a lot: print(). Perhaps there are a lot of hidden features about this function that we do not know of?

Here is the function and its parameters according to the documentation:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Looking at this, we can see that there are five parameters. The first is a positional argument, and four other keyword arguments (with default value).

The description gives more details about what the function does:

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

Let us dissect the description.

Firstly, objects. Notice the asterisk (*) in front of objects. This means that you can give multiple objects separated by commas. So technically you can do this:

>>> print("put", 123, "and", [4,5,6], (7,8,9), {"key1": "value1"}, None, True)
put 123 and [4, 5, 6] (7, 8, 9) {'key1': 'value1'} None True

Then the description says the objects are “separated by sep” (default is a blank space). So we can customise the sepator. So let’s say do not want any spaces between the objects:

>>> print("I", "hate", "social", "distancing", sep="")
Ihatesocialdistancing

The next thing is says that it is “followed by end (default is “\n”). Did you realise that each time you use print(), it automatically prints your object in a new line? There may be times when you do not want it to do that!

print("Countdown...", end=" ")
for i in range(10):
    print(i, end=" ")
print("... and we're off!!")

Then we have the file argument:

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used

So this means that print() prints to sys.stdout (that is your terminal output). Technically, it also means that you can print to somewhere else, like a text file. We will talk about file manipulation later on, but for now, this will print your object(s) to a file called output.txt in the same directory as your script.

print("I'm appearing in a file! You will not see me in your terminal!", 
      file=open("output.txt", "w"))

We will skip discussing the last parameter flush, it is not quite important or revelant.

So, now that you have discovered the hidden features of print(), try exploring more built-in functions and reading their documentation! Throughout your degree, you will spend a lot of your development time trying to read and understand the documentations functions for any libraries that you will be using!

Bonus tip: if you see square brackets surrounding parameters in the documentation, e.g. float([x]), it just means that the parameter is optional. So float() returns 0.0 according to the documentation.