Please note: All the code are executed in Python 2.7.
1. Function definition taking two arguments & returning one
def fun(arg1,arg2):
return arg1 + ' ' + arg2
fun("Hello","World")
OUTPUT>> Hello World
2. Function definition with Default Arguments
def default_fun( name, location = 'Bangalore'):
print name, location
default_fun('zekelabs', 'Mumbai')
OUTPUT>> zekelabs Mumbai
default_fun('zekelabs')
OUTPUT>> zekelabs Bangalore
PS: Default arguments can be only at end of function definition. No non-default arguments can be after default arguments
3. Function definition 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 a function call doesn't matter. This is very useful with large number of function arguments
OUTPUT>> Jill Bangalore male
5. Function definition 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 definition 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 definition for accepting key-value pair as a 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
kwargs_func(name='Zekelabs',location='Bangalore',price=1000)
OUTPUT>>
Key price : Value 1000
Key name : Value Zekelabs
Key location : Value Bangalore
8. Function definition for unpacking when a 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}
kwargs_unpack_func(**kwargs)
Artificial Intelligence course from zekeLabs has been featured as one of the Top-10 AI courses in a recent study by Analytics India Magazine in 2018. Here is what makes our courses one of the best in the industry.
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
Almost every programming language provides a straightforward mechanism to check the End of File. Python does not have a very straightforward and intuitive way to check EOF.Following code can help you find EOF for all practical purposes.
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 .
Python provides various machine learning libraries such as sci-kit learn, Pylearn2, Theano, Pyevolve, Tensorflow etc. Apache Spark for analytics is now being widely adopted for machine learning. Following are the areas where Apache Spark gets hand with re...
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...