The Many Functions

Python has many, many built-in functions. You could never learn them all in this course. You will learn a few, but it’s far more important that you learn how to learn more about functions.

Learning about a Function

Documentation of the built-in `len` function: Name: len. Description: The function "len" returns the number of characters in a string (its length). Syntax: len(a_string). Parameters: a_string (String): The string to calculate the length of. Returns: Integer. Example: len("Hello World").

Good programmers create documentation for their functions. Documentation is extra information about a function that makes it clear how it works. There are many ways to write documentation, but they usually have a few things in common.

Function Documentation

The same documentation as before, with the following parts circled: Name, Description, Syntax, Parameters, Returns, Example.

You should look for these parts: the function’s name, a description, its syntax, its parameters, its return value, and an example of how to use it.

Parameters and Arguments

The call calculate_area(3, 10), with 3 and 10 labelled "Arguments". Below it, the function's documentation: Name: calculate_area. Syntax: calculate_area(length, width). Parameters: length (Integer): The length of the rectangle. width (integer): The width of the rectangle. In the syntax line, length and width are labelled "Parameters".

The values passed into a function are its arguments. The documentation gives the formal names for those values, its parameters — the names used to refer to them inside the function. In the example above, 3 and 10 are the arguments; the documentation tells us the corresponding parameters are length and width.

Nesting Function Calls

print(len("Hello World")) with arrows pointing from the string to len, and from len to print, showing how the block-based version print[length of["Hello World"]] nests the same way.

A common task is nesting function calls inside other function calls. When nesting function calls, you read from the inside outward. This can be a little unintuitive, but it may help to treat the parentheses the same way you would in math. It’s critical to remember, though, that the parentheses are there because they call the function — not because they’re doing math.

Chaining Methods

"hello world".title().count('o'), with arrows pointing from the string to title, and from the result of title() to count.

When you need to call several methods on the same string, it works a little differently. As shown above, you repeat the period, name, and parentheses each time. By placing them side by side, you keep using the same value, transforming it in turn from left to right.

Functions, Methods, and Operators

string_length = len("hello " + " world ".strip()), with "string_length =" labelled Assignment Statement, "len(" labelled Function Call, "+" labelled String Addition, and ".strip()" labelled Method Call.

Since calls are just another kind of expression, you can freely combine them with each other. The example above combines string addition, a string method call, and a function call. Notice the order of execution here — it can be tricky to follow.

The order of operations here is that the method call happens first, removing the space characters from around the " world " string literal. Then, the two string literals are added together. Finally, the outer function call is executed with the "hello world" value; this counts the number of characters in the string, which is the value 11. Note that neither 11 nor "hello world" is written anywhere in the code; both are computed at runtime.

Input/Output

You already know two functions. print takes in arguments and writes them to the console. input reads from the console and returns it as a string value. Both are a little unusual, because they have side effects: input displays a text box for the user, and print writes text to the console.

int, float, str, bool

Four other functions that are useful to know are int, float, str, and bool. These four functions are used for type conversion, or type casting values. They can consume one value of any type and convert that value to the new type.

int and float

# int: Consumes anything, returns an integer (if possible)
print(int("27"))

# float: Consumes anything, returns a float (if possible)
print(float("29.4"))

# This will fail with an error!
print(int("two"))

For integers and floats, this requires the value to make sense: you can convert strings of digits but not strings with letters. Those values will cause an error if you attempt to convert them.

str

# str: Consumes anything, returns a string (always possible)
print(str(27) + " is a string now and can be safely added!")
print(str(True))

The str function, however, allows you to convert ANYTHING to a string. Using the str function is particularly important since we cannot add values of different types; if we want to use values of other types in a string, we must convert the values first.

bool

# bool: Consumes anything, returns a Boolean (always possible)
print(bool("Hello World"))
print(bool(""))

Similarly, the bool function also allows you to convert anything to a Boolean value. We’ll discuss the rules for how values get converted in the next chapter, but for now you just need to know that everything can be converted to a string or Boolean.

Summary