In [1]:
# Function
# function is a block of code - which can be called anynumber of times.
In [2]:
def my_func():
print "hello world!!!"
In [5]:
print type(my_func)
print my_func
my_func
Out[5]:
In [6]:
# every function has a return value
# if you have no return value you get 'None'
print my_func()
In [9]:
# return is not a print statement.
# return marks the end of the function.
def my_func():
return "hello world!!!"
print "hello onces"
print "hello twice"
print "hello thrice"
In [10]:
print my_func()
In [11]:
# namespace/scope(local/global)
In [14]:
# Variables defined inside a function local variables/namespaces.
# The variables exist during the run time of the function.
# locals - inbuild function
# Use case I:
def my_func():
print locals()
x = 10
print locals()
return x
In [15]:
print my_func() # 10
print x #
In [16]:
# globals
# we look first into locals and then into global.
# resolving happens from within the function.
# use case II:
y = 10
def my_func():
print locals()
return y
In [17]:
print my_func() # Novalue,10
print y # 10,10
In [25]:
# use case III:
balance = 0
def deposit():
balance = 0
print locals()
balance = balance + 1000
return balance
def withdraw():
balance = 0
print locals()
balance = balance - 300
return balance
In [21]:
# provided you dont put balance=0 within function
'''
def deposit():
print locals()
balance = balance + 1000
return balance
'''
print deposit()
In [23]:
print deposit()
In [24]:
print withdraw()
In [26]:
print withdraw()
In [27]:
# global
balance = 0
def deposit():
global balance
print locals()
balance = balance + 1000
return balance
def withdraw():
global balance
print locals()
balance = balance - 300
return balance
In [28]:
print deposit()
print withdraw()
In [29]:
# *,**,*args,**kwargs
# map,filter,lamba
In [1]:
# functional arguments
# you pass arguments as objects.
def my_add(a,b):
result = a + b
return result
In [4]:
# positional based arguments
print my_add(10,11)
print my_add("python"," rocks")
print my_add(" rocks","python")
In [7]:
# key based arguments
print my_add(a="python",b=" rocks")
print my_add(b=" rocks",a="python")
In [8]:
# *,**
In [10]:
my_values=[11,22]
# i want to pass my_values to my function my_add
#a,b=my_values
print my_add(*my_values)
print my_add(my_values)
In [19]:
# **
my_values={'a':'linux','b':'rocks'}
my_values1={'a':'linux','c':'rocks'}
# a = my_values['a']
# b = my_values['b']
print my_add(**my_values)
print my_add(**my_values1)
In [12]:
# *args
In [13]:
print help(max)
In [14]:
print max(-1,-2,-3,-4,-5) # -1
print max(11,33,44) #44
print max(22,10) #22
In [17]:
# args - returns a tuple
def gmax(*args):
big = -1
for value in args:
if value > big:
big = value
return big
In [18]:
print gmax(-1,-2,-3,-4,-5) # -1
print gmax(11,33,44) #44
print gmax(22,10) #22
In [20]:
# **kwargs
In [26]:
# kwargs returns a dictionary
def callme(**kwargs):
if 'name' in kwargs:
print "my name is {}".format(kwargs['name'])
if 'gender' in kwargs:
print "my gender is {}".format(kwargs['gender'])
if 'maiden' in kwargs:
print "your mother maiden name is {}".format(kwargs['maiden'])
if 'location' in kwargs:
print "your location is {}".format(kwargs['location'])
return True
In [27]:
print callme(name='kumar',gender='m')
print callme(name='kumar',maiden='vijaya',location='hyd')
print callme(location='hyd',gender='m')
In [30]:
# default
def multi(num,limit=10):
for value in range(1,limit+1):
print "{0:2d} * {1:2d} = {2:3d}".format(num,value,num*value)
In [31]:
multi(5)
In [32]:
multi(5,limit=5)
In [34]:
multi(5,5)
In [35]:
# use case
#https://screenshots.en.sftcdn.net/en/scrn/20000/20678/putty-7.jpg
def putty(hostname,port=22):
pass
In [36]:
# ssh -> putty(hostname)
# telnet -> putty(hostname,23)
In [37]:
## map,filter and lambda
In [38]:
def my_square(a):
return a * a
In [40]:
print my_square(25)
print my_square(23)
In [41]:
print help(map)
In [49]:
'''
def my_square(a):
return a * a
'''
print map(my_square,range(11,21))
print filter(my_square,range(11,21))
In [43]:
# filter
# true function - return value
# false function - no return value
In [44]:
def my_even(a):
if a % 2 == 0:
return 'even'
In [45]:
print my_even(100) # even # True
print my_even(101) # None # False
In [46]:
print help(filter)
In [48]:
'''
def my_even(a):
if a % 2 == 0:
return 'even'
'''
print filter(my_even,range(11,21))
print map(my_even,range(11,21))
In [ ]:
# lambda
# creating functions on fly.
In [52]:
print map(my_square,range(11,21))
print map(lambda a:a*a,range(11,21))
print filter(my_even,range(11,21))
print filter(lambda a:a%2==0,range(11,22))
In [53]:
# function is a first class object.
In [54]:
def my_add(a,b):
return a + b
def my_sub(a,b):
return a - b
def my_extra(func,a,b):
return func(a,b)
In [55]:
print my_extra(my_add,11,22)
print my_extra(my_sub,33,22)
In [56]:
# function within a function
def upper():
def inner():
x = 1
return x
return inner()
print upper() # 1,None
print inner() # 1,1
In [61]:
# function within a function
# function closures - All the variables available to the function during the defination of
# function are imported automaticaly.
def upper():
x = 1
def inner():
return x
return inner
foo = upper()
print type(foo)
print foo
print x
In [60]:
print foo()
In [ ]: