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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.