Python method or function
Function or method is similar things or use for similar purposes. In short Function or Method execute one or more statements of code for accomplishing a single objective, It takes zero or more input called arguments and may or may not return value.
Objectives
-
Python function definition and calling
-
Python function with arguments and type
-
Python function with arguments default value
-
Python function with return value
-
Python function call using keyword arguments
-
Python function arbitrary arguments, *args
-
Python function arbitrary keyword arguments, **kwargs
-
Python function pass statement
Python function definition and calling
Code
# Definition of a function
def first_function():
print("Hi, This is my first function.")
# Called the defined function
first_function()
Output
Hi, This is my first function.
Process finished with exit code 0
Python function with arguments and type
Code
# Definition of a function with 2 parameter / argument
def sum_two_number(first_number: int, second_number):
print(first_number + second_number)
# Called the defined function
sum_two_number(10, 10.50)
Output
20.5
Process finished with exit code 0
In the function
-
first_number: int : Represent the argument data type
-
second_number : Not specified the argument data type
Note : Specifying of argument data type not mandatory, but good practise
Python function with arguments default value
Argument default value means if you not provide the argument value then it will take the pre-defined value, otherwise will take the provided value. Code
# Definition of a function with argument default
def i_am_a(designation="Programmer"):
print("I am a " + designation)
# Called the defined function
print("Call the function without pass argument : ", end="")
i_am_a()
print("Call the function with pass argument : ", end="")
i_am_a("Doctor")
Output
Call the function without pass argument : I am a Programmer
Call the function with pass argument : I am a Doctor
Process finished with exit code 0
Python function with return value
Code
# Definition of a function with 2 parameter / argument and return value
def sum_two_number(first_number: int, second_number):
return first_number + second_number
# Called the defined function
sum_of_2 = sum_two_number(20, 10.50)
print(sum_of_2)
Output
30.5
Process finished with exit code 0
Python function call using keyword arguments
Basically you have to maintain order of the arguments during the function calling, otherwise it will not work perfectly. But You can also send arguments with the key = value syntax. This way the order of the arguments does not matter.
Code
# Definition of a function with multiple argument
def my_info(first_name, last_name, email):
print(first_name + " " + last_name + " " + email)
# Called the defined function
my_info(email="hmtmcse.com@gmail.com", last_name="Mia", first_name="Touhid")
Output
Touhid Mia hmtmcse.com@gmail.com
Process finished with exit code 0
Python function arbitrary arguments, *args
Sometimes you may have to take many arguments, but you don’t know how many arguments that will be passed into your function, add a * before the argument name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly
Code
# Definition of a function with unlimited argument
def my_info(*args):
print(args[0] + " " + args[1] + " " + args[2])
# Called the defined function
my_info("hmtmcse.com@gmail.com", "Mia", "Touhid", "Some other")
Output
hmtmcse.com@gmail.com Mia Touhid
Process finished with exit code 0
Python function arbitrary keyword arguments, **kwargs
Arbitrary keyword arguments similar to arbitrary arguments, the only difference is you have to send arguments with the key = value syntax, and add two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly
Code
# Definition of a function with unlimited keyword argument
def my_info(**kwargs):
print(kwargs["first_name"] + " " + kwargs["last_name"] + " " + kwargs["email"])
# Called the defined function
my_info(email="hmtmcse.com@gmail.com", last_name="Mia", first_name="Touhid")
Output
Touhid Mia hmtmcse.com@gmail.com
Process finished with exit code 0
Python function pass statement
Python function definitions cannot be empty, but if you for some reason have a function definition with no functionality, put in the pass statement to avoid getting an error.
Code
def empty_function():
pass