def
isn’t a function, it defines a function, and is one of the basic keywords in Python.
For example:
def square(number):
return number * number
print square(3)
Will display:
9
In the above code we can break it down as:
- def – Tells python we are declaring a function
- square – The name of our function
- ( – The beginning of our arguments for the function
- number – The list of arguments (in this case just one)
- ) – The end of the list of arguments
- : – A token to say the body of the function starts now
- The following newline and indent then declare the intentation level for the rest of the function.
It is just as valid (although uncommon) to see:
def square(number): return number * number
In this case, as there is no indentation the entirety of the function (which in this case is just one line) runs until the end of line. This is uncommon in practise and not considered a good coding style.
1
solved What is a DEF function for Python [closed]