Functions

Functions — a block of code which is executed only when it is called.

def new_func():
    print("Hi, Welcome")

new_func() #calling the function

To define a Python function, the def keyword is used.

*args

>>> def concat(*args, sep="/"):
...     return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
  • used when you aren’t sure how many arguments are going to be passed to a function, or if you want to pass a stored list or tuple of arguments to a function.
  • Arbitrary number of arguments.
  • These arguments will be wrapped up in a tuple.

**kwargs

# Python program to illustrate 
# *kwargs for variable number of keyword arguments 

def myFun(**kwargs): 
	for key, value in kwargs.items(): 
		print ("%s == %s" %(key, value)) 

# Driver code 
myFun(first ='Geeks', mid ='for', last='Geeks')

used when you don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.

Used to pass a keyworded, variable-length argument list.

Functions — Structure map

Clickable & Draggable!

Functions — Related pages: