Python

Python Functions with Examples1 min read

You can save a block of code in a function so that you can re-use it.

This is a very important concept in programming.




Here’s an example of an “add_numbers” function.

Output:

 

 

 

Here’s a more complicated function using a for loop and if statement.

 

Output:

We can re-use that function for a different food list.

Output:

 

To assign the result of a function to a variable we need to use return.

Output:

Functions can also call other functions.

Let’s use the “add_numbers” function inside the “subtract_numbers” function.

Output:

 

Leave a Comment