Function in Python

Why should we learn to use Python’s built-in functions? Because they can save us a lot of time when we’re programming.

Intro to Built-In/Creating Functions

Welcome to Level 4.

We’re going to explore the built-in functions in Python. We’ve already talked about the print and type functions, and now we’ll introduce more built-in functions.

Next, let’s discuss the “why.” Why should we learn to use Python’s built-in functions?

Because they can save us a lot of time when we’re programming.

What if Python doesn’t have a built-in function that we need? That’s okay, because in Chapter 4, we’ll also teach you how to create your own custom functions.

Lastly, How are we going to learn about functions together? We’ll do several exercises to understand how to use the built-in functions and how to define and use our own functions.

Basic Built in Python Functions that Use Math

So far, we’ve talked about two types of functions: type and print.

Remember, the type function tells us if an object is a number, a string, or another data type.

In this chapter, we’ll cover some basic built-in Python functions used for math.

In Microsoft Excel, you can type =min() to find the minimum value in a range of cells.

Similarly, in Python, we have the min function.

You use it like this: min(value1, value2, value3). The max function works the same way to find the maximum value.

To find the absolute (positive) value of a number, we use the abs function: abs(value).

When using the round function, the syntax is round(number, digits). If we want to round a number to 2 decimal points, we write: a = round(a, 2).

Input Built in Python Function

Now that we understand how to use some math functions, let’s talk about a function that doesn’t involve math:

the input function. This is my favorite because it allows us to interact with the user.

Here is an example of how to use the input function:

				
					name = input("What is your name?")

				
			

When we run this code, it prompts the user to enter their name and assigns whatever they type to the variable name.

This way, we can gather information from the user and use it in our program.

Creating Basic Custom Functions

Now that we understand how to use some built-in Python functions, let’s learn how to make custom functions. This is a lot of fun. Here’s how we do it:

STEP 1: Start by typing def, which stands for define.

STEP 2: Name your function and include (): at the end, like this:

				
					def my_function():

				
			

STEP 3:  On the next line, indent and type your code. If you don’t indent, the function won’t work.

STEP 4:  To call the function and run it, type the function name followed by () without indenting, like this:

				
					my_function()
				
			

Passing Parameters in Function

Let’s talk about passing parameters, which means sending data to a function when we call it.

We’ll create code that asks for a person’s name and age, and then call a function that tells us how old the person will be in one year.

STEP 1: Define the function and include parameters for name and age:

				
					def future_age(name, age):
    new_age = age + 1
    print(name + " will be " + str(new_age) + " next year.")

				
			
STEP 2: Ask for the person’s name and age:
				
					name = input("Enter your name: ")
age = int(input("Enter your age: "))

				
			
STEP 3: Call the function with the name and age parameters:
				
					future_age(name, age)

				
			

Remember to indent the code inside the function.

When calling the function, make sure the call is not indented. Also, when you concatenate (attach) strings together, you need to convert integers to strings using str().

This ensures that the data types match, preventing errors.

Creating Advanced Functions (Keyword and Default Values)

So far, with our custom functions, we’ve assumed that the arguments were passed in order.

For example, if we have a function that takes a name and age, we pass the arguments in that order:

				
					def future_age(name, age):
    new_age = age + 1
    print(name + " will be " + str(new_age) + " next year.")

future_age("Chris", 51)

				
			
What if we wanted to pass the arguments not in order? We can use keyword arguments for this. Here’s how we do it:
				
					future_age(age=51, name="Chris")

				
			

We can also set default values for our function parameters.

This way, if we call the function without some arguments, the function will use the default values:

				
					def future_age(name="Yoda", age=900):
    new_age = age + 1
    print(name + " will be " + str(new_age) + " next year.")

# Calling the function without any arguments
future_age()
# Output: Yoda will be 901 next year.

# Calling the function with one argument
future_age(name="Luke")
# Output: Luke will be 901 next year.

# Calling the function with both arguments
future_age(name="Leia", age=30)
# Output: Leia will be 31 next year.

				
			
This way, our function is more flexible and can handle various cases of argument passing.

EXERCISE

Ahsoka is 1.70 meters tall, Yoda is 0.66 meters tall and R2D2 is 1.09 meters tall

Step 1: Create the variables of height for the 3 characters
Step 2: Create a print line of code that prints this:
    The average height is [put the average height here].
    The maximum height is [put the maximum height here].
    The minimum height is [put the maximum height here].
Step 3: This is tricky, but fun, you have to create additional variables for the average, minimum, and maximum heights

Please complete the following steps:
Step 1: Create code that asks for your name and then your age.
Step 2: Print this:
    Your name is [your name]. Your age is [your age].

Please complete the following steps:

    Step 1: Define a function called age_of_rey
    Step 2: In the function, ask “How old is Rey? ”
    Step 3: In the function print Rey’s age is 19.
    Step 4: Call the function and enter Rey’s age, which is 19.

Please complete the following steps:

Step 1: Ask who was on the Titanic?
Step 2: Ask what year is it currently?
Step 3: Print: Jack and Rose were on the Titanic, which sunk 112* years ago.
*This number will be higher if you take this course after 2024.

Play Video about finding Video

Other Lecture