Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python def Keyword



The Python def keyword is used to define a function. Along with def keyword, function's name followed by parenthesis containing function's parameter(s) is used to define a function. Please visit function page for more information on this topic.

Syntax

#Defining function
def function_name(parameters):
      statements

#Calling function
function_name(parameters)

Example: A function with no parameter

In the example below, a function called MyFunction is created to print Hello World!. The function requires no parameters to execute.

def MyFunction():
  print("Hello World!.")

MyFunction()

The output of the above code will be:

Hello World!.

Example: A function with a parameter

In the example below, the function called MyFunction is created which takes one parameter to execute.

def MyFunction(name):
  print("Welcome to Python Programming! " + name +".")

MyFunction("John")
MyFunction("Marry")
MyFunction("Sam")

The output of the above code will be:

Welcome to Python Programming! John.
Welcome to Python Programming! Marry.
Welcome to Python Programming! Sam.

❮ Python Keywords

5