Get startedGet started for free

Built-in functions

1. Built-in functions

Welcome! I'm George, and I'll be your host!

2. What we'll cover

In this course, we'll dive into fundamental programming topics including built-in functions, modules, and packages, before progressing to building our own custom functions!

3. Functions we know

Let's recap the functions we already know. First there's print, which displays outputs. There's type, which tells us what a variable, value, or function is. We also know range, which can be used with a for loop to generate numbers.

4. max() and min()

Software and data engineers often work with numbers. Luckily, Python has lots of useful built-in numeric functions to make our lives easier! Here, we have a list of sales transactions. If we want to find the largest transaction, we can call the max function, passing our list. Likewise, to get the smallest transaction, we use the min function.

5. sum() and round()

To find the total number of sales, we use the sum function. That's a lot of decimal places, let's round this! We store the calculation as a variable called total_sales. We then use the round function, providing the value to be rounded followed by the number of decimals to round to - in this case, two.

6. Nested functions

Notice that we called sum, stored the result as a variable, and then rounded that variable. However, it is possible to call a function inside a function instead. Here, we call the round function, inside which we calculate total sales by calling the sum function. We close the parentheses for the sum function, then add a comma, after which we include the number two representing how many digits to round the result to. Printing the results confirms the same result.

7. len()

Another useful function is len, which counts the number of elements. Calling len on our sales list confirms there are seven transactions. We now have the tools to perform other calculations like the average or mean. We can get the sum of sales and divide it by the number of transactions, producing a mean of around 105 dollars.

8. len()

We can use len on other data types, such as strings. It counts the number of characters, including spaces. This could be useful if we need to enforce rules around the number of characters in a password for a website. We can also use it to find the number of key-value pairs in a dictionary. It will also work with sets and tuples but isn't compatible with integers, floats, or booleans.

9. sorted()

Another useful function is sorted, which sorts a variable in ascending order. Here, we use it on our sales list as well as on a string. Notice that the string "George" gets sorted by uppercase characters alphabetically first, followed by lowercase characters.

10. help()

If we want to know more about a function, then we can call the help function and pass that function, like here for sorted. Help provides information about how to use a function, such as what information we need to provide to it. In this case, it tells us we provide an iterable and mentions we can provide values to arguments called key and reverse, which will change the output. We can also call the help function on data types and structures!

11. Benefits of functions

Functions allow us to perform complex tasks with less code. Let's demonstrate this by looking at how to find the sum of a list of values if the sum function didn't exist.

12. Benefits of functions

We would have to create a variable to store our calculation, starting at zero. We then loop through sales and, with each iteration, add each sale to the sales_count variable. Printing the variable during each loop confirms the same final result, but it took way more code! The sum function is reusable, as we just change the values that we pass to it. It's also much shorter, cleaner, and less prone to mistakes.

13. Functions cheat sheet

This cheat sheet summarizes the functions we've just covered. Python has lots of additional built-in functions, which you can access from the URL in the slide.

14. Let's practice!

Let's have fun with functions!