Home »
Python
Python def Keyword
By IncludeHelp Last updated : December 07, 2024
Description and Usage
The def is a keyword (case-sensitive) in python, it is used to define a function, it is placed before the function name (that is provided by the user to create a user-defined function).
Syntax
Syntax of def keyword:
def function_name:
function definition statements
Sample Input/Output
Input:
# an empty function
def myFunc():
pass
# printing its type
print("type of myFunc:", type(myFunc()))
Output:
type of myFunc: <class 'NoneType'>
Example 1
Define an empty function using pass statement and print its type/p>
# python code to demonstrate example of
# def keyword
# an empty function
def myFunc():
pass
# main code
# printing it's type
print("type of myFunc:", type(myFunc()))
Output
type of myFunc: <class 'NoneType'>
Example 2
Define a function to find sum of two numbers
# python code to demonstrate example of
# def keyword
# function to add two numbers
# function definition
def addNumbers(x, y):
return (x+y)
# main code
a = 10
b = 20
# finding sum
result = addNumbers(a, b)
# print statement
print("sum of ", a, " and ", b, " is = ", result)
Output
sum of 10 and 20 is = 30