How to use Python Functions.

Python functions are reusable code blocks that perform specific tasks. Learn to use built-in functions like max to find the highest value in a list, and round to round numbers with optional precision. Use the help function to check documentation and understand which arguments are required versus optional.
Chapters
In this video, I'm going to introduce you to Functions. Functions aren't entirely new for you. Actually, you've already used them. Type, for example, is a function that returns a type of a value. But what is a function? Simply put, a function is a piece of reusable code aimed at solving a particular task. You can call functions instead of having to write code yourself. Maybe an example can clarify things here. Suppose you have the list containing only the heights of your family. Say now that you want to get the maximum value of this list. Instead of writing your own piece of Python code that goes through the list and finds the highest value, you can also use Python's max function. This is one of Python's built in functions. Just like type, we simply pass fam to max inside parentheses. The output makes sense. 1.89, the highest number in the list. Max worked kind of like a black box here. You passed it a list. Then the implementation of Max that you don't know did its magic and produced an output. How MAX actually did this is not important to you. It just does what it's supposed to. And you didn't have to write your own code, which made your life easier. Of course, it's also possible to also assign this result of a function call to a new variable, like in here. Now, tallest is just like any other variable, you can use it to continue your fancy calculations. Another one of these built in functions is round. It takes two first, a number you want to round, and second, the precision with which to round. So how many digits behind the decimal point you want to keep? Say you want to round 1.68 to one decimal place. The first input is 1.68, the second input is 1. You separate the inputs with a comma. But there's more. It's perfectly possible to call the round function with only one input like this. This time Python figured out that you didn't specify the second input and automatically chooses to round the number to the closest integer. To understand why both approaches work, let's open up the documentation. We can do this with yet another function help as follows. It appears that round takes two inputs. In Python, these inputs, also called arguments, have names number and ndigits. When you call a function round with these two inputs, Python matches the inputs to the number is set to 1.68 and Ndigits is set to 1. Next, the round function does its calculations with number and n digits as if they are variables in a Python script. We don't know exactly what code Python executes. What is important though, is that the function produces an output, namely the number 1.68 rounded to one decimal place. If you call the function round with only one input, Python again tries to match the inputs to the arguments. There's no input to match the n digits argument, though. Luckily the internal machinery of the round function knows how to handle this. When ndigits is not specified, the function simply rounds to the closest integer and returns that integer. That's why we got the number two. How was I so sure that calling the function with a single input would work? Well in the documentation there are square brackets around the comma and the and digits here. This tells us that you can call around in this form as well as in this one. In other words, and digits is an optional argument. Actually, Python offers yet another way to show that a function has optional arguments, but that's something for the exercises by now you have an idea about how to use max and round. But how could you know that a function such as round exists in Python in the first place? Well, this is something you will learn with time. Whenever you are doing a rather standard task in Python, you can be pretty sure that there is already a function that can do this for you. In that case, you should definitely use it. Just do a quick Internet search and you'll find the function you need with a nice usage example. And there is of course datacamp, where you'll also learn about powerful functions and how to use them. Get straight to it in the interactive exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







