Built-in functions
1. Built-in functions
Welcome! I'm Jasmin, and I'll be your host as we explore essential Python programming skills!2. What we'll cover
In this course, we'll master built-in functions, modules, and packages before creating custom functions and handling errors to keep our code running smoothly. To get the most from this course, you should be familiar with the Python foundations covered in the prerequisite course.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 to repeat a task several times.4. Built-in functions
Developers frequently need to process data. Python's built-in functions make this easier, helping us build features with less code. Let's say we're building a performance monitoring dashboard for a food delivery app. The operations team needs to track restaurant preparation times, and we'll use Python's built-in functions to create this feature.5. max() and min()
Here are today's preparation times from one restaurant in minutes. To find the longest preparation time, we call the `max()` function, passing our list and printing the result. Likewise, to get the shortest preparation time, we use the `min()` function.6. sum() and round()
Next, to calculate the total preparation time, we use the `sum()` function. Let's round this! We store the calculation as `total_time`, then use the `round()` function, providing the value to be rounded, followed by the number of decimals - in this case, one.7. len()
Another useful function is `len()`, which counts the number of elements. Calling `len()` on our preparation times confirms there are seven orders. We now have the tools to calculate the average preparation time. We get the sum of preparation times and divide by the number of orders using the division operator, which is a forward slash. This means on average, it takes around 28 minutes to prepare an order.8. len()
We can use `len()` on other data types, such as strings. It counts the number of characters, including spaces. For example, the length of our partner "Burger Hub" is 10 because of the included space.9. sorted()
Another useful function is `sorted()`, which sorts a variable in ascending order. Here, we use it on our preparation times to review them from shortest to longest. We can also use `sorted()` on strings. Notice that the string "George" gets sorted by uppercase characters alphabetically first, followed by lowercase characters.10. 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 if the `sum()` function didn't exist.11. Benefits of functions
We would have to create a variable to store our calculation, starting at zero. We then loop through preparation times, adding each time to `time_count`. Recall the handy `+=` operator that we use to add time to the current total. Printing the variable during each loop confirms the same final result, but it took way more code! The `sum()` function is reusable - we just change the values we pass to it. It's also much shorter, cleaner, and less prone to mistakes.12. Let's practice!
Let's have fun with functions!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.