Python Def: Create Functions

Python def is the command you put at the front of the functions you’re defining.

You can call def when creating functions, or class methods. Make sure that your function code is on the next child indent.

There is not a lot of magic to this one!

Fun fact: def stands for ‘define’ as in, define a function or define a method.

Let’s take a look at a python def code sample

Python Def

Def is the keyword you put at the start of your function. No big mysteries here.

Here we use def when making a regular function

In [1]:
def my_func():
    print ("This is my function!")
In [2]:
This is my function!

Here we use def when making a method in a class

In [3]:
class Student():
    def set_age(self, age):
        self.age = age
In [5]:
Bob = Student()
In [6]:

Link to code above

Check out more Python Vocabulary on our Glossary Page