Creating a function :
def function_name():
….action
function_name() <— calling the function
function_name <— referencing the function
def function_ip(ip):
….print(“My ip is:{}”.format(ip)) or print(f’My ip is :{ip}’)
….return
function_ip(‘10.1.1.1′)
def function_ip(ip, mask, gtw):
….print(“My ip is:{} mask is : {} and gtw is :{}”.format(ip, mask, gtw))
….print(f’My ip is :{ip},mask is :{mask} and gtw is : {gtw}’)
return
function_ip(‘10.1.1.1’, ‘255.255.255.0’,’10.1.1.1254′)
function_ip(mask=’255.255.255.252′, gtw=’10.1.1.254′, ip=’10.1.1.1′)
My ip is:10.1.1.1 mask is : 255.255.255.0 and gtw is :192.168.1.1
My ip is :10.1.1.1 mask is :255.255.255.0 and gtw is : 192.168.1.1
My ip is:10.1.1.1 mask is : 255.255.255.252 and gtw is :10.1.1.254
My ip is :10.1.1.1 mask is :255.255.255.252 and gtw is : 10.1.1.254
Its is possible to create function with default values :
( remember non default arguments must be placed 1st )
def function_ip(gtw, ip=’10.10.10.1′, mask=’255.255.255.0′):
….print(“My ip is:{} mask is : {} and gtw is :{}”.format(ip, mask, gtw))
….print(f’My ip is :{ip},mask is :{mask} and gtw is : {gtw}’)
….return
function_ip(‘10.1.1.254’)
My ip is:10.10.10.1 mask is : 255.255.255.0 and gtw is :10.1.1.254
My ip is :10.10.10.1,mask is :255.255.255.0 and gtw is : 10.1.1.254
Lists :
my_list = [‘10.1.1.1’, ‘username’, ‘password’]
function_ip(*my_list) <— the * make the function go through each element.
Dictionary :
my_dict = {‘ip:”10.1.1.1’, ‘mask’: ‘255.255.255.0’, ‘gtw’: ‘10.1.1.254’}
function_ip(**my_list) <— convert to the key value pair not to a single element.