In [ ]:
# *,**,*args,**kwargs
In [1]:
def my_func(a,b):
return a + b
print my_func("linux","rocks")
In [7]:
my_list = ["linux"," rocks"]
my_list1 = ["linux","rocks","fine"]
my_list2 = ["linux"]
# please unpack the elements "linux" and "rocks" into both a and b.
print my_func(*my_list) # unpacking your list to a funtion.
print my_func(*my_list1)
In [8]:
print my_func(*my_list2)
In [ ]:
# **
In [10]:
def my_func(a,b):
return a + b
print my_func(a="linux",b=" rocks") # keyword arguments
In [12]:
# dictionary
my_values = {'a':"linux",'b':" rocks"}
print my_func(**my_values)
In [13]:
my_values1 = {'a':"linux",'b':" rocks",'c':'choco'}
print my_func(**my_values1)
In [1]:
# *args
# More than one argument.
print help(max)
In [3]:
print max(23,24,25,61)
print max(1,61)
In [4]:
# gmax
# if a function defination has *args and if you return args, you get a tuple of values.
def gmax(*args):
return args
In [5]:
print gmax(21,31,51,63,66)
print gmax(31,21)
In [10]:
# *args basically passes n number of functions.
def gmax(*args):
big = 0
print args
for value in args:
print "following are the values : big {} , value {}".format(big,value)
if value > big:
big = value
return big
In [11]:
print gmax(21,31,51,63,66)
print gmax(31,21)
In [12]:
# **kwargs
def callme(**kwargs):
return kwargs # dictionary of values.
print callme(name="kumar",age="45")
print callme(name="kumar",maiden="vijaya")
print callme(gender='m',location="hyd")
In [16]:
def callme(**kwargs):
if 'name' in kwargs:
print "my name is {}".format(kwargs['name'])
if 'age' in kwargs:
print "my age is {}".format(kwargs['age'])
if 'maiden' in kwargs:
print "my mother name is {}".format(kwargs['maiden'])
if 'location' in kwargs:
print "The location is {}".format(kwargs['location'])
if 'gender' in kwargs:
print "the gender is {}".format(kwargs['gender'])
# main
#callme(name="kumar",age="45")
#callme(name="kumar",maiden="vijaya")
callme(gender='m',location="hyd")
In [20]:
# some defination of functions
def foo():
pass
In [21]:
foo
Out[21]:
In [22]:
print type(foo)
In [23]:
print foo
In [28]:
# function within a functions.
# the local variables are resticted to function.
# the lifetime of the local variables is during the runtime of the function.
def upper():
x = 1 # local variable for upper() function.
def inner(): # inner() function is a local variable/function within your upper() function.
return x # x is going to be fetched from global value x of upper() function.
print locals()
return inner() # you are returing the value of inner() function
In [29]:
print upper() # 1
In [19]:
print inner() # Error
In [30]:
# function within a functions.
# the local variables are resticted to function.
# the lifetime of the local variables is during the runtime of the function.
# how did inner() function get the value x.
# function closures: During the defination of the function , all the global and the local variables available will be present
# even when we return the address of the function.
def upper():
x = 1 # local variable for upper() function.
def inner(): # inner() function is a local variable/function within your upper() function.
return x # x is going to be fetched from global value x of upper() function.
print locals()
return inner # you are returing the address of the inner function.
In [31]:
new = upper()
In [32]:
print new
In [27]:
print new()
In [33]:
# function is a first class object.
# int,float,str as first class objects.
def add(x,y):
return x + y
def sub(x,y):
return x - y
def extra(func,x,y):
return func(x,y)
In [34]:
print extra(add,22,23)
print extra(sub,25,23)
In [ ]:
# map,filter and lambda
In [35]:
print help(map)
In [36]:
def my_square(a):
return a * a
In [ ]:
# truth of function: if a function returns a value, its called the truth of a function.
# if a function return None its called as NOT A TRUE function.
In [37]:
print my_square(2)
In [38]:
print my_square(3)
In [40]:
print map(my_square,[22,25,27,29,33])
In [46]:
print filter(my_square,[22,25,27,29,33])
In [44]:
# filter
print help(filter)
In [42]:
def my_even(a):
if a % 2 == 0:
return 'even'
In [43]:
print my_even(2) # TRUTH OF A FUNCTIONS
print my_even(3) # FALSE OF A FUNCTION.
In [45]:
print filter(my_even,[1,2,3,4,5])
In [47]:
print map(my_even,[1,2,3,4,5])
In [51]:
# lambda : writing nameless functions on fly.
print map(my_square,[22,25,27,29,33])
print map(lambda a:a*a,[22,25,27,29,33])
print filter(my_even,[1,2,3,4,5])
print filter(lambda a:a % 2 == 0,[1,2,3,4,5])
In [ ]:
# https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt