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
def my_func():
print ("This is my function!")
my_func()
Here we use def when making a method in a class
class Student():
def set_age(self, age):
self.age = age
Bob = Student()
Bob.set_age(20)
Bob.age
Check out more Python Vocabulary on our Glossary Page