Deep Dive into Understanding Functions in Python

Deep Dive into Understanding Functions in Python

Deep Dive into Understanding Functions in Python  Awantik Das
Posted on May 22, 2017, 12:41 p.m.

functions in python

Please note: All the code are executed in Python 2.7.

1. Function defination taking two arguments & returning one

 def fun(arg1,arg2):

      return arg1 + '  ' + arg2


   OUTPUT>> Hello World

2. Function defination with Default Arguments

   def default_fun( name, location = 'Bangalore'):

        print  name, location

   default_fun('zekelabs', 'Mumbai')

   OUTPUT>> zekelabs Mumbai


   OUTPUT>> zekelabs Bangalore

   PS: Default arguments can be only at end of function defination. No non-default arguments can be after default arguments

3. Function defination with keyword based arguments

    def keyword_func( name, location, gender ):

          print name, location, gender

   keyword_func(location='Bangalore', name='Jill', gender='male')

   PS: Order of arguments in function call doesn't matter. This is very useful with large number of function arguments

   OUTPUT>> Jill Bangalore male

5. Function defination for variable arguments - accepting argument as a tuple

    def varargs_func( *args ):

         for arg in args:

             print arg, # This will print all the arguments passed to the function


    varargs_func('Jack','Jill','went', 'to','Malabar','hill')

    OUTPUT>> Jack Jill went to Malabar hill

    varargs_func('Raja', 'and', 'Rancho')

    OUTPUT>> Raja and Rancho

6. Function defination for unpacking - passing list/tuple as argument

   def unpacking_args_func(name,location,gender):
          print name,location,gender

   info = ['Zekelabs','Bangalore','male']
   unpacking_args_func( *info )  #passing list

   OUTPUT>>Zekelabs Bangalore male

7. Function defination for accepting key-value pair as dictionary. You will see lot of use cases in Python frameworks code like Django 

 def kwargs_func(**kwargs):
       #kwargs is a dictionary. We can fetch data by kwargs['key'] as well
       for k,v in kwargs.items():
            print 'Key ',k, ' : ','Value ',v



  Key price : Value 1000
  Key name : Value Zekelabs
  Key location : Value Bangalore

8. Function defination for unpacking when dictionary is passed.

    Pass a dictionary and make sure all keys are arguments in function

   def kwargs_unpack_func(name,location,price):
           print name,location,price

   kwargs = {'name':'Awantik','location':'Bangalore','price':2000}


Awantik Das is a Technology Evangelist and is currently working as a Corporate Trainer. He has already trained more than 3000+ Professionals from Fortune 500 companies that include companies like Cognizant, Mindtree, HappiestMinds, CISCO and Others. He is also involved in Talent Acquisition Consulting for leading Companies on niche Technologies. Previously he has worked with Technology Companies like CISCO, Juniper and Rancore (A Reliance Group Company).

Keywords : Python spark

Recommended Reading

Object Model in Python - Understanding Internals

The object model of Python is something very less discussed but important to understand what happens under the cover. Understanding this before diving into python makes journey smooth

How do I check end of file (EOF) in python?

Almost every programming language provides a straightforward mechanism to check the End of File. Python however does not have a very straightforward and intuitive way to check EOF. However following piece of code can help you find EOF for all practical purp...

What is future prospects of being a Django developer in India?

Apart from Training Django, due to increasing corporate requirement I am given assignments to interview candidates for Python & Django. Sharing my understanding of entire scenario from candidates prospective or corporate prospective.

When Python has ML libraries, why do you need Apache Spark for analytics?

Python provides various machine learning libraries such as sci-kit learn, Pylearn2, Theano, Pyevolve, Tensorflow etc. However Apache Spark for analytics is now being widely adopted for machine learning. Following are the areas where Apache Spark gets upper ...

How should I start learning Python?

If this is the question in your mind, you have already started to fall in love with Python. Technical learning should be always layered. Start with the easiest book like Think Python & end with bible like Learning Python. In Python, there is always deep...