Python Tutorial - Writing user-defined functions

Welcome to the Course: Python Data Science Toolbox

My name is Hugo Bound Anderson and I'm a data scientist at The Data Camp. In this course, we'll learn to write our very own functions and apply these newfound skills to questions that commonly arise in data science contexts. Specifically, in this video and the interactive exercises that follow, you'll learn to do the following: defined functions without parameters, defined functions with single parameters, and defined functions that return a single value. Let's begin by checking out Python's built-in function STR.

STR is a built-in function in Python that accepts an object such as a number and returns a string object. You can assign a call to STR to a variable to store its return value. While built-in functions are cool, as a data scientist you'll need functions that have functionality specific to your needs. Fortunately, you could define your own functions in Python.

Defining Functions via an Example

To define a function, we begin with the keyword def followed by the function name. For example, let's define a function named square. This is then followed by a set of parentheses and a colon. This piece of code is called a function header to complete the function definition. Now, whenever this function is called, the code in the function body is run.

To write the function body, we start with the keyword def again, but this time without the function name or parameters. In our example, let's say we want to square a value, say 4. We assign a new variable, new_value, the value of 4 to the power of 2 and then print it out.

Now, our square function does not have any parameters within the parentheses. To add that functionality, we can add a parameter to the function definition in between the parentheses. Let's see how this works with an example. We've added a parameter of value and in the new function body, the new variable new_value takes the square of value, which is then printed out.

With this addition, we can now square any number that we pass to the function square as an argument. A quick word on parameters and arguments: when you define a function, you write parameters in the function header; when you call a function, you pass arguments into the function. The function square now accepts a single parameter and prints out its squared value.

Returning Values from Functions

What if we don't want to print that value directly? Instead, we want to return the squared value and assign it to some variable? You can have your function return the new value by adding the return keyword followed by the value to return. Let's see this in action with an example.

We can now assign to a variable num the result of the function call. There's another essential aspect of writing functions in Python: docstrings. Docstrings are used to describe what your function does, such as the computation it performs or its return values. These descriptions serve as documentation for your function so that anyone who reads your function's docstring understands what your function does without having to trace through all the code in the function definition.

Docstrings are placed in the immediate line after the function header and a place between triple quotation marks. An appropriate docstring for our function square is "returns the square of a value". You've now just learned the basics of defining your own functions. It's time to try your hand at defining and using your very own functions in the next few interactive exercises.

"WEBVTTKind: captionsLanguage: enwelcome to the course my name is Hugo Bound Anderson and I'm a data scientist the data camp in this course the first of the Python data science toolbox courses you'll learn to write your very own functions and you'll have the opportunity to apply these newfound skills to questions that commonly arise in data science contexts specifically in this video and in the interactive exercises that follow it you will learn to do the following defined functions without parameters defined functions with single parameters and defined functions that return a single value in the next section you'll learn how to pass multiple arguments to functions as well as return multiple values from them let's begin let's first check out pythons built-in function STR which accepts an object such as a number and returns a string object you can assign a call to STR to a variable to store its return value while built-in functions are cool as a data scientist you'll need functions that have functionality specific to your needs fortunately you could define your own functions in Python will now see how to define functions via an example a function that squares a number the function named square will be perfect for this to define the function we begin with the keyword def followed by the function name square this is then followed by a set of parentheses and a colon this piece of code is called a function header to complete the function definition let's write the function body by squaring a value say 4 and printing the output right now our square function does not have any parameters within the parentheses we will add them later now whenever this function is called the code in the function body is run in this case new value is assigned the value of 4 to the power of 2 and then print it out you can call the function as you do with pre-built functions square this should yield the value 16 what if you wanted to square any other number besides 4 though to add that functionality you add a parameter to the function definition in between the parentheses here you see that we've added a parameter of value and in the new function body the new variable new value takes the square of value which is then printed out we can now square any number that we pass to the function square as an argument a quick word on parameters and arguments when you define a function you write parameters in the function header when you call a function you pass arguments into the function the function square now accepts a single parameter and prints out its squared value but what if we don't want to print that value directly and instead we want to return the squared value and assign it to some variable you can have your function return the new value by adding the return keyword followed by the value to return now we can assign to a variable num the result of the function call as you see here there's another essential aspect of writing functions in Python docstrings docstrings are used to describe what your function does such as the computation it performs or its return values these descriptions serve as documentation for your function so that anyone who reads your functions docstring understands what your function does without having to trace through all the code in the function definition function docstrings are placed in the immediate line after the function header and a place between triple quotation marks an appropriate docstring for our function square is returns the square of a value you've now just learned the basics of defining your own functions now it's your turn in the next few interactive exercises you will try your hand at defining and using your very own functionswelcome to the course my name is Hugo Bound Anderson and I'm a data scientist the data camp in this course the first of the Python data science toolbox courses you'll learn to write your very own functions and you'll have the opportunity to apply these newfound skills to questions that commonly arise in data science contexts specifically in this video and in the interactive exercises that follow it you will learn to do the following defined functions without parameters defined functions with single parameters and defined functions that return a single value in the next section you'll learn how to pass multiple arguments to functions as well as return multiple values from them let's begin let's first check out pythons built-in function STR which accepts an object such as a number and returns a string object you can assign a call to STR to a variable to store its return value while built-in functions are cool as a data scientist you'll need functions that have functionality specific to your needs fortunately you could define your own functions in Python will now see how to define functions via an example a function that squares a number the function named square will be perfect for this to define the function we begin with the keyword def followed by the function name square this is then followed by a set of parentheses and a colon this piece of code is called a function header to complete the function definition let's write the function body by squaring a value say 4 and printing the output right now our square function does not have any parameters within the parentheses we will add them later now whenever this function is called the code in the function body is run in this case new value is assigned the value of 4 to the power of 2 and then print it out you can call the function as you do with pre-built functions square this should yield the value 16 what if you wanted to square any other number besides 4 though to add that functionality you add a parameter to the function definition in between the parentheses here you see that we've added a parameter of value and in the new function body the new variable new value takes the square of value which is then printed out we can now square any number that we pass to the function square as an argument a quick word on parameters and arguments when you define a function you write parameters in the function header when you call a function you pass arguments into the function the function square now accepts a single parameter and prints out its squared value but what if we don't want to print that value directly and instead we want to return the squared value and assign it to some variable you can have your function return the new value by adding the return keyword followed by the value to return now we can assign to a variable num the result of the function call as you see here there's another essential aspect of writing functions in Python docstrings docstrings are used to describe what your function does such as the computation it performs or its return values these descriptions serve as documentation for your function so that anyone who reads your functions docstring understands what your function does without having to trace through all the code in the function definition function docstrings are placed in the immediate line after the function header and a place between triple quotation marks an appropriate docstring for our function square is returns the square of a value you've now just learned the basics of defining your own functions now it's your turn in the next few interactive exercises you will try your hand at defining and using your very own functions\n"