In [1]:
# what is a function?
# A block of code which can be called multiple times.
In [2]:
# defination of the function.
# Every function has a return value.
# Lets say,if i have no return value.. i get None.
def foo(): # function with optional parameters
print "hello world!!"
In [3]:
print foo()
In [4]:
print foo()
In [7]:
# We get a return value for the truth of a function.
# truth of a function can be any value.
# if we dont have a return value we get None.
# return is not a print statement.
# return marks the end of the function.
def foo(): # function with optional parameters
return "hello world!!"
print "line1"
print "line2"
print "line3"
In [8]:
print foo()
In [12]:
## variable scope/namespace
# variables defined inside a function are available only during the function runtime.
# Literally,there is no syntax to access the variable defined inside a function.
# locals(): provides you the namespace of the local variables.
# use case I
def my_fun():
x = 1
print locals()
return x
In [13]:
print my_fun()
In [11]:
print x
In [16]:
# use case II:
# globals -> this show your all the variables which are available globally to us.
# if i have no variable in localscope i will look into global scope.
y=10 # global variable
def my_fun():
print locals()
return y
In [17]:
print my_fun() # 10
In [18]:
print y
In [ ]:
In [5]: globals()
Out[5]:
{'In': ['',
u'y = 10',
u'def my_fun():\n print locals()\n return y',
u'print my_fun()',
u'print y',
u'globals()'],
'Out': {},
'_': '',
'__': '',
'___': '',
'__builtin__': <module '__builtin__' (built-in)>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': 'Automatically created module for IPython interactive environment',
'__name__': '__main__',
'_dh': [u'/home/khyaathi'],
'_i': u'print y',
'_i1': u'y = 10',
'_i2': u'def my_fun():\n print locals()\n return y',
'_i3': u'print my_fun()',
'_i4': u'print y',
'_i5': u'globals()',
'_ih': ['',
u'y = 10',
u'def my_fun():\n print locals()\n return y',
u'print my_fun()',
u'print y',
u'globals()'],
'_ii': u'print my_fun()',
'_iii': u'def my_fun():\n print locals()\n return y',
'_oh': {},
'_sh': <module 'IPython.core.shadowns' from '/usr/local/lib/python2.7/dist-packages/IPython/core/shadowns.pyc'>,
'exit': <IPython.core.autocall.ExitAutocall at 0x7f4623d17110>,
'get_ipython': <bound method TerminalInteractiveShell.get_ipython of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x7f4623cb1290>>,
'my_fun': <function __main__.my_fun>,
'quit': <IPython.core.autocall.ExitAutocall at 0x7f4623d17110>,
'y': 10}
In [6]: globals()['y']
Out[6]: 10
In [7]: globals()['my_fun']
Out[7]: <function __main__.my_fun>
In [19]:
# Use case III:
# local variables are given higher precendence than global variables.
z = 10
def my_func():
z = 11
print locals()
return z
In [20]:
print my_func()
In [21]:
print z
In [22]:
# use case IV:
balance = 0
def my_deposit():
balance = balance + 5000
return balance
In [23]:
print my_deposit()
In [24]:
balance = 0
def my_deposit():
balance = 0
balance = balance + 5000
return balance
In [25]:
print my_deposit()
In [26]:
def my_withdraw():
balance = 0
balance = balance - 1000
return balance
In [27]:
print my_withdraw()
In [28]:
# global keyword
balance = 0
def my_deposit():
global balance
print locals()
balance = balance + 5000
return balance
def my_withdraw():
global balance
print locals()
balance = balance - 1000
return balance
In [29]:
print my_deposit()
print my_withdraw()
In [30]:
print balance
In [31]:
# functional arguments
def my_add(a,b):
return a + b
In [37]:
# arguments on position based.
print my_add("linux"," rocks")
print my_add(" rocks","linux")
print my_add(11,22)
In [35]:
# arguments on key based.
print my_add(a='linux',b=' rocks')
print my_add(b=' rocks',a='linux')
In [36]:
print my_add(x='linux',y='rocks')
# TODO : google task today.
# how are the agruments passed.
# 1) as references
# 2) as values
In [38]:
# default
# default is not a keyword... you can write any variable name.
def my_multi(num,default=10):
for value in range(1,default+1):
print "{0:2d} * {1:2d} = {2:3d}".format(num,value,num*value)
In [40]:
my_multi(2)
my_multi(3,5)
In [47]:
# *,**,*args,**kwargs
def my_add(a,b):
return a + b
# *
my_value1 = [11,22]
my_value2 = [11,22,33]
print my_add(my_values)
In [45]:
print my_add(*my_value1)
In [46]:
print my_add(*my_value2)
In [50]:
## **
my_values = {'a':11,'b':22}
my_value2 = {'c':11,'a':11,'b':22}
print my_add(my_values)
In [51]:
print my_add(**my_values)
In [52]:
print my_add(**my_value2)
In [ ]:
## *args
In [53]:
print help(max)
In [55]:
print max(-1,-2,-3,-4,-5)
print max(11,22)
print max(33,-1,8,100)
In [56]:
# args is a tuple of values.
def gmax(*args):
return args
In [57]:
print gmax(-1,-2,-3,-4,-5)
print gmax(11,22)
print gmax(33,-1,8,100)
In [60]:
def gmax(*args):
big = -1
for value in args:
if value > big:
big = value
return big
In [61]:
print gmax(-1,-2,-3,-4,-5)
print gmax(11,22)
print gmax(33,-1,8,100)
In [62]:
# A call me app
# kwargs is a dictionary.
def callme(**kwargs):
print kwargs
In [63]:
callme(name='kumar',loc='hyd')
callme(name='kumar',mobile='a.b.c')
callme(name='kumar',mobile='a.b.c',maiden='vijaya')
In [68]:
def callme(**kwargs):
if 'name' in kwargs:
print "my name is {}".format(kwargs['name'])
if 'loc' in kwargs:
print "my loc is {}".format(kwargs['loc'])
if 'mobile' in kwargs:
print "my mobile is {}".format(kwargs['mobile'])
if 'maiden' in kwargs:
print "my maiden in {}".format(kwargs['maiden'])
In [69]:
callme(name='kumar',loc='hyd')
callme(name='kumar',mobile='a.b.c')
callme(name='kumar',mobile='a.b.c',maiden='vijaya')
In [ ]:
# use case:
#https://images.sftcdn.net/images/t_optimized,f_auto/p/6a5919fa-96d1-11e6-b8e8-00163ec9f5fa/1811083446/putty-screenshot.jpg
def putty(hostname,port=22):
pass
# putty(google.com) # 22
# putty(google.com,23) # telnet
In [1]:
## Function within a function
def upper():
y = 1
def inner():
return y
return inner() # calling the inner function.
In [2]:
print upper() # 1
In [3]:
print inner() #1,error
In [8]:
##
def foo():
pass
print foo
print foo()
foo
Out[8]:
In [19]:
## function closures
# when ever i define a function(inner).. i have access to both local and global variables
# and they will be available even when i return them as address.
def upper():
y = 1
def inner():
return y
return inner
In [20]:
foo = upper()
In [21]:
print foo
In [22]:
print foo() #1,None,
In [ ]:
## functions are first class objects.
# str,int,float
In [24]:
def my_extra(my_func,x,y):
return my_func(x,y)
def my_add(x,y):
return x + y
def my_sub(x,y):
if x > y:
return x - y
else:
return y - x
In [25]:
print my_extra(my_add,11,12)
print my_extra(my_sub,32,23)
In [26]:
# map,filter and lambda
print help(map)
In [27]:
def my_square(a):
return a * a
In [29]:
print my_square(2)
print my_square(25)
In [31]:
print map(my_square,[2,44,55,66,77,88])
In [32]:
## filter
print help(filter)
In [33]:
def even(a):
if a % 2 == 0:
return 'even'
In [34]:
print even(2)
print even(3)
In [35]:
print filter(even,range(1,11))
In [38]:
# lambda - creating function on fly.
print map(my_square,[2,44,55,66,77,88])
print map(lambda a:a*a,[2,44,55,66,77,88])
print filter(my_square,[2,44,55,66,77,88])
In [39]:
print filter(even,range(1,11))
print filter(lambda a:a%2==0,range(1,11))
print map(even,range(1,11))
In [ ]: