Composing a Function in Python

Step-by-step Python programming tutorial

Are you interested in learning Python? Art, a Python instructor at Noble Desktop, provides a detailed tutorial on how to compose your own Python function.

Key Insights

  • In Python, a function is a block of reusable code that can be used repeatedly.
  • The creation of a function begins with the keyword 'def', which stands for 'define', followed by the name of the function. The name should reflect the function's purpose.
  • Functions can take arguments, which can be defined within the function, as demonstrated by Art with 'a' as 6 and 'b' as 7.
  • Instead of printing the result of a function, the 'return' operator is used as a function always returns something.
  • To invoke a function, you need to call it by using the function name.
  • To save the result of a function, assign it to a variable. These variables can then hold the result from the function.

Python Workflow

  1. Set Up venv

    python -m venv .venv — isolated environment per project.

  2. Install Dependencies

    pip install -r requirements.txt — pin versions for reproducibility.

  3. Write & Test

    Write functions; test with pytest as you go.

  4. Run & Deploy

    python script.py locally, deploy to your platform of choice.

Play video

Video Transcription

Hi, my name is Art and I teach Python at Noble Desktop. Today I'm going to show you how to compose your own function.

A function is a block of reusable code, something we can use over and over again. Let's create our first function.

Start with the keyword 'def', which stands for 'define', and then come up with a name for the function. The name should reflect the purpose of the function, so I'm going to call mine 'add'. This function takes two arguments (a and b). I want to define a as 6 and b as 7. Then I want to do a total of a plus b.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Now we usually print stuff, as humans we want to see what's going on. But in this case, we will use the 'return' operator, because a function always returns something. So I'm going to 'return' total.

Now I have this function, but nothing is going on because I need to call it to invoke the function. So I'm going to use the function name 'add'. You see I'm calling this function and I'm getting the result.

Keep in mind that 'return' should be the last statement within the function, nothing can be done after 'return'. Also, if I want to save the result of the function, I need to assign it to a variable, like 'variable_one' and 'variable_two'.

Now, these variables will hold the result from the function.

In my next video, I'll show you how to call a function within a function. See you in my next video!

Video Transcription:

Full Video Transcript

Introduction to Python Functions

Hi, my name is Arthurin and I teach Python. Today I'm going to show you how you could compose your own function—a Python function. What is a function? A function is a block of reusable code, which means something that we could use over and over again.

Creating Your First Function

Let's create our first function. First, you need to begin with the keyword "def." Def stands for define, and we want to define a function. Then you need to come up with any name for your function—pretty much any name will do. You could use "banana," however the name of the function should reflect the purpose of the function.

I'm going to call my function "add." This function uses parentheses, colon, and then indentation, because now all the statements that we're going to write here will be blocked as a part of the function. Then I'm going to define A as 6 and B as 7, and then I want to do total equals A plus B.

Understanding the Return Statement

Now usually we print stuff because we're humans and we want to see what's going on there. But in this case, we will use the return operator because functions always return stuff. So return, and we want to return total. Now you see I got this function. I run my cell, and this function is quietly sitting there. Nothing is going on because we need to call this function, or the right word would be to invoke the function.

I'm going to use function name "add," and you see I'm calling this function and I'm getting that result. Keep in mind that sometimes you also want to do something else, maybe print something. If you want to do print, you cannot do it after return—nothing could be done after return. So you need to squeeze the print statement right here before the return if you want to see what's going on.

If I rerun the cell, you see I got 13. This 13 comes from line five, and the output comes from line six. So return should be the last statement within the function—nothing could be done after the return.

Using Function Arguments

This function kind of makes no sense. Why? Because if I wanted to know how much would be 6 plus 7, then I would save this 13 with some kind of variable so I could use it later. I don't want to hard code this value, so I will take them as arguments: A and B.

Now you see if I run this function, it's not working because it says, "Hey, if you want to use function add, you need to pass two arguments: A and B." So now we could call this function with 5 and 6, or we could use it again and call it with any other numbers like 50 and 60. By the way, I'm going to remove the print statement because I no longer need it.

Storing Function Results

That's how it works. Also, if you want to save that result from the function, you need to assign it to a variable. Let's say variable one and variable two. Now variable one will hold the result from that function, and variable two will hold the result from where we're calling this function "add" with 15 and 60.

In the next video, I'll show you how to call a function within a function. So I'll see you in my next video.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram