Home » Python

def keyword with example in Python

Python def keyword: Here, we are going to learn about the def keyword with example.
Submitted by IncludeHelp, on April 14, 2019

Python def keyword

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 of def keyword

    def function_name:
        function definition statements

Example:

    Input:
    # an empty function
    def myFunc():
        pass

    # printing its type
    print("type of myFunc:", type(myFunc()))

    Output:
    type of myFunc: <class 'NoneType'>

Python examples of def keyword

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.