Python def Keyword
Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In python def keyword is the most used keyword.
def function_name: function definition statements...
Use of def keyword:
- In the case of classes, the def keyword is used for defining the methods of a class.
- def keyword is also required to define the special member function of a class like __init__().
The possible practical application is that it provides the feature of code reusability rather than writing the piece of code again and again we can define a function and write the code inside the function with the help of the def keyword. It will be more clear in the illustrated example given below. There can possibly be many applications of def depending upon the use cases.
Example 1: Use of def keyword.
In this example, we are going to create a user define a function using def keyword.
subtraction of 90 and 50 is = 40
Example 2: User defines a function with first 10 prime numbers.
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 23
Example 3: User defines a function with a factorial number.
The factorial of 6 is 720