In [ ]:
# function
# one single program - 1000 lines
# block of the code and call it when required.
#
In [1]:
def my_func():
print "hello world"
In [8]:
def my_func1():
return "hello world"
print "hey today is monday"
print "today we are going to have fun"
In [4]:
print type(my_func)
print my_func
my_func
Out[4]:
In [5]:
# run the code
# you function has return value.
# either you return or you get a none value.
# Marks the end of the function.
# can we use return more than once in a function. yes, with a conditional statements.
print my_func()
In [9]:
print my_func1()
In [10]:
# namespaces
# local or global variables
In [14]:
# variable defined inside a function are called local variables.
# They come into action during the run time of the function.
# Lifespan of the local variables is during the run time of the function.
# can i access the local variables outside the function ? NO
def my_new():
x = 1
print locals()
return x
In [15]:
y = 10
def my_new():
print locals()
return y
print my_new()
In [14]:
y = 10
def my_new():
y = 20
print locals()
return y
print my_new()
In [15]:
print my_new()
In [13]:
print x
In [19]:
# Scope Resolution
# First look locally and then globally.
# locals -> give the namespace/variables available to us locally.
# globals -> give the namespace/variables available to us globally.
x = 10
def my_new():
x = 2
print locals()
return x
print globals()
In [ ]:
# 'x': 10,
# 4th line from bottom.
In [17]:
print my_new()
In [20]:
print my_new()
In [21]:
print x
In [8]:
# 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
# Main
print deposit()
print balance
print deposit()
print withdraw()
print balance
In [9]:
# functional paremeters
def my_add(a,b):
return a + b
In [10]:
# positional functional arguments
print my_add(10,20)
print my_add('linux','rocks')
print my_add('rocks','linux')
In [11]:
# key based functional arguments
print my_add(b='rocks',a='linux')
In [18]:
# default arguments
def multi(num,kumar=10):
for value in range(1,kumar+1):
print "{} * {} = {}".format(num,value,num*value)
multi(2)
multi(3,5)
In [ ]:
# putty - http://cache.filehippo.com/img/ex/1125__putty1.png
# def putty(hostname,port=22)
# putty(hostname) # ssh
# putty(hostname,23) # telnet
In [20]:
# *,**,*args,**kwargs
In [21]:
# *
def my_add(a,b):
return a + b
my_list = [10,20]
#(a,b) = my_list
# a = my_list[0]
# b = my_list[1]
print my_add(my_list)
In [22]:
print my_add(*my_list)
In [23]:
my_list2= [10,20,30]
print my_add(*my_list2)
In [24]:
# **
def my_add(a,b):
return a + b
my_dict = {'b':20,'a':10}
print my_add(**my_dict)
In [25]:
my_dict1 = {'b':30,'c':20}
print my_add(**my_dict1)
In [26]:
# *args
print help(max)
In [28]:
print max(-5,-1)
print max(22,33,14,51,62,74,89,20)
In [30]:
#
def gmax(*args):
big=-1
for value in args:
if value > big:
big = value
return big
print gmax(-5,-1)
print gmax(22,33,14,51,62,74,89,20)
In [31]:
# **kwargs
def my_callme(**kwargs):
print kwargs
my_callme(name='kumar',gender='m')
my_callme(name='kumar',mobile='x.u.y.z')
my_callme(location='hyd',mother='laxmi')
In [1]:
def my_callme(**kwargs):
if 'name' in kwargs:
print "your name is {}".format(kwargs['name'])
if 'gender' in kwargs:
print "your gender is {}".format(kwargs['gender'])
if 'mobile' in kwargs:
print "your mobile is {}".format(kwargs['mobile'])
if 'location' in kwargs:
print "your location is {}".format(kwargs['location'])
if 'mother' in kwargs:
print "your mother name is {}".format(kwargs['mother'])
In [2]:
my_callme(name='kumar',gender='m')
my_callme(name='kumar',mobile='x.u.y.z')
my_callme(location='hyd',mother='laxmi')
In [6]:
# function witin a function
def outer():
x = 1
def inner():
return x
print locals()
return inner() # calling the the inner function
# Main
print outer() # 1
print inner() # inner is local to outer function so is not available.
In [9]:
# Function Closures
# All the variables available toa function during defination time, are available even if we
# return it as a address.
def outer():
x = 1
def inner():
return x
print locals()
return inner # you are returning the inner function address.
# Main
foo = outer()
print foo,type(foo)
print foo() # 1
print x
#print inner() # inner is local to outer function so is not available.
In [10]:
# Functions are first class object.
def add(x,y):
return x + y
def sub(x,y):
return x - y
def extra(func,x,y):
return func(x,y)
print extra(add,2,4) # 6
print extra(sub,4,2) # 2
In [11]:
# map,filter and lambda
print help(map)
In [12]:
# map
def square(a):
return a * a
print square(2) # 4
print square(25) # 625
In [13]:
print map(square,[21,22,23,24,25])
# function,arguement sequence
In [14]:
# filter
print help(filter)
In [16]:
def even(a):
if a % 2 == 0:
return 'even'
print even(2) # even # True
print even(3) # None # False
In [17]:
print filter(even,[21,22,23,24,25])
In [21]:
# some fun
print map(square,range(1,11))
print filter(square,range(1,11))
print map(even,range(1,11))
print filter(even,range(1,11))
In [23]:
# lambda
# creation of a nameless function. creation of a function on fly.
print map(lambda a:a*a,range(1,11))
print filter(lambda a:a%2==0,range(1,11))
In [ ]: