×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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

Comments and Discussions!

Load comments ↻





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