In [ ]:
# Function
# A block of code,which can be called repetatively.

In [1]:
# defining a function

def my_func():
    print "hey there!!"

In [2]:
print my_func   # address of the function
print my_func() # calling the function.
my_func         # my_func is part of our __main__ program. similar to void main in c++.


<function my_func at 0x7f843c220c80>
hey there!!
None
Out[2]:
<function __main__.my_func>

In [ ]:
# every function has a return value.
# if a function has no retrun value we get None.
# return marks the end of the function.

In [5]:
def my_func():
    return "hey there!!"
    print "line1"
    print "line2"
    print "line3"

In [6]:
print my_func()


hey there!!

In [ ]:
# function scope/namespaces/variables

In [ ]:
# variable defined inside a function are restricted to the function.
# variable inside function/local scope variables get activated during run time of function.
# Lifespan of a variable is during the runtime of the function.

In [7]:
# case 1
def my_func():
    a = 1
    return a

In [ ]:
print my_func() # 1

In [8]:
print a


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-9d7b17ad5387> in <module>()
----> 1 print a

NameError: name 'a' is not defined

In [12]:
# case 2
# locals - show the local variables
a = 10 # global variable
def my_func():
    print locals()
    return a

In [13]:
print my_func() # 10
print a         # 10


{}
10
10

In [14]:
# case 3 
# locals - show the local variables
# local variabels are given higher precedence than global variable.
a = 10 # global variable
def my_func():
    a = 2
    print locals()
    return a

In [15]:
print my_func() # 2
print a         # 10


{'a': 2}
2
10

In [16]:
# globals
print globals()


{'_dh': [u'/home/tcloudost/Documents/git_repositories/python-batches/batch-59'], '__': '', '_i': u'print my_func() # 2\nprint a         # 10', 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f843c2e9290>, '__builtins__': <module '__builtin__' (built-in)>, '_ih': ['', u'# defining a function\n\ndef my_func():\n    print "hey there!!"', u'print my_func\nprint my_func()\nmy_func', u'def my_func():\n    return "hey there!!"', u'print my_func()', u'def my_func():\n    return "hey there!!"\n    print "line1"\n    print "line2"\n    print "line3"', u'print my_func()', u'def my_func():\n    a = 1\n    return a', u'print a', u'# case 2\na = 10 # global variable\ndef my_func():\n    return a', u'print my_func() # 10\nprint a         # 10', u'print my_func() # 10\nprint a         # 10', u'# case 2\n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    print locals()\n    return a', u'print my_func() # 10\nprint a         # 10', u'# case 3 \n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    a = 2\n    print locals()\n    return a', u'print my_func() # 2\nprint a         # 10', u'# globals\nprint globals()'], '__builtin__': <module '__builtin__' (built-in)>, '_2': <function my_func at 0x7f843c220c80>, '__name__': '__main__', '___': '', '_i15': u'print my_func() # 2\nprint a         # 10', '_': <function my_func at 0x7f843c220c80>, 'a': 10, '_sh': <module 'IPython.core.shadowns' from '/usr/local/lib/python2.7/dist-packages/IPython/core/shadowns.pyc'>, '_i12': u'# case 2\n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    print locals()\n    return a', '_i11': u'print my_func() # 10\nprint a         # 10', '_i9': u'# case 2\na = 10 # global variable\ndef my_func():\n    return a', '_i8': u'print a', '_i7': u'def my_func():\n    a = 1\n    return a', '_i6': u'print my_func()', '_i5': u'def my_func():\n    return "hey there!!"\n    print "line1"\n    print "line2"\n    print "line3"', '_i4': u'print my_func()', '_i3': u'def my_func():\n    return "hey there!!"', '_i2': u'print my_func\nprint my_func()\nmy_func', '_i1': u'# defining a function\n\ndef my_func():\n    print "hey there!!"', '__doc__': 'Automatically created module for IPython interactive environment', '_i13': u'print my_func() # 10\nprint a         # 10', 'my_func': <function my_func at 0x7f84427b1140>, '_iii': u'print my_func() # 10\nprint a         # 10', '_i10': u'print my_func() # 10\nprint a         # 10', 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f843c2e9290>, 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7f8444f4d950>>, '_ii': u'# case 3 \n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    a = 2\n    print locals()\n    return a', 'In': ['', u'# defining a function\n\ndef my_func():\n    print "hey there!!"', u'print my_func\nprint my_func()\nmy_func', u'def my_func():\n    return "hey there!!"', u'print my_func()', u'def my_func():\n    return "hey there!!"\n    print "line1"\n    print "line2"\n    print "line3"', u'print my_func()', u'def my_func():\n    a = 1\n    return a', u'print a', u'# case 2\na = 10 # global variable\ndef my_func():\n    return a', u'print my_func() # 10\nprint a         # 10', u'print my_func() # 10\nprint a         # 10', u'# case 2\n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    print locals()\n    return a', u'print my_func() # 10\nprint a         # 10', u'# case 3 \n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    a = 2\n    print locals()\n    return a', u'print my_func() # 2\nprint a         # 10', u'# globals\nprint globals()'], '_i16': u'# globals\nprint globals()', '_i14': u'# case 3 \n# locals - show the local variables\na = 10 # global variable\ndef my_func():\n    a = 2\n    print locals()\n    return a', '_oh': {2: <function my_func at 0x7f843c220c80>}, 'Out': {2: <function my_func at 0x7f843c220c80>}}

In [26]:
# global: keyword

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 [18]:
# i see this error when the code was
'''
def deposit():
    balance = balance + 1000
    return balance
'''
print deposit()


---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-18-f63fab9016ab> in <module>()
----> 1 print deposit()

<ipython-input-17-bc0de6ae4759> in deposit()
      4 
      5 def deposit():
----> 6     balance = balance + 1000
      7     return balance
      8 

UnboundLocalError: local variable 'balance' referenced before assignment

In [27]:
print deposit()


{}
1000

In [28]:
print withdraw()


{}
700

In [29]:
print balance


700

In [ ]:
# passing arguments,key arguments,default arguments
# *,**,*args,**kwargs

In [4]:
# passing arguments

# position based.
def my_add(a,b):
    return a + b

print my_add(22,23)
print my_add('linux',' rocks')
print my_add(' rocks','linux')


45
linux rocks
 rockslinux

In [6]:
# key based
def my_add(a,b):
    return a + b

print my_add(b=22,a=23)
print my_add(a='linux',b=' rocks')
print my_add(b=' rocks',a='linux')


45
linux rocks
linux rocks

In [7]:
# default keys
# default is a variable not key word.

def my_multi(num,default=10):
    for value in range(1,default+1):
        print "{} * {} = {}".format(num,value,num*value)

In [8]:
print my_multi(2)


2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
None

In [9]:
print my_multi(3,5)


3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
None

In [10]:
print my_multi(default=5,num=10)


10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
None

In [11]:
# http://cache.filehippo.com/img/ex/1125__putty1.png

def putty(hostname,port=22):
     print "{},{}".format(hostname,port)
    
putty('www.google.com')
putty('www.amazon.com')
putty('www.icicibank.com',port=80)
putty('www.yahoo.com',port=23)


www.google.com,22
www.amazon.com,22
www.icicibank.com,80
www.yahoo.com,23

In [15]:
# *

def my_add(a,b):
    return a + b

my_list = [22,23]
my_list1 = ['linux','rocks']
my_list2 = ['my','linux','rocks']

In [13]:
print my_add(my_list)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-dcdd83570984> in <module>()
----> 1 print my_add(my_list)

TypeError: my_add() takes exactly 2 arguments (1 given)

In [16]:
# * is unpacking the list to elements of a function.
print my_add(*my_list)
print my_add(*my_list1)
print my_add(*my_list2)


45
linuxrocks
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-00d6eed815b0> in <module>()
      2 print my_add(*my_list)
      3 print my_add(*my_list1)
----> 4 print my_add(*my_list2)

TypeError: my_add() takes exactly 2 arguments (3 given)

In [19]:
# **
my_dict = {'a':11,'b':22}
my_dict1 = {'a':'linux','b':'rocks'}
my_dict2 = {'a':'linux','c':'rocks'}

print my_add(**my_dict)
print my_add(**my_dict1)
print my_add(**my_dict2)


33
linuxrocks
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-14a3cc0bf749> in <module>()
      6 print my_add(**my_dict)
      7 print my_add(**my_dict1)
----> 8 print my_add(**my_dict2)

TypeError: my_add() got an unexpected keyword argument 'c'

In [20]:
# *args
print help(max)


Help on built-in function max in module __builtin__:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value
    
    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

None

In [22]:
print max(77,21,31,48,99)
print max(-1,-2,-44,-55)
print max(2,4)


99
-1
4

In [23]:
# gmax
# args a tuple of values passed to *args.
def gmax(*args):
    print args

In [26]:
def gmax(*args):
    big=-1
    for value in args:
        if value > big:
            big = value
    return big

In [27]:
print gmax(77,21,31,48,99)
print gmax(-1,-2,-44,-55)
print gmax(2,4)


99
-1
4

In [28]:
# **kwargs
# kwargs returns a dictionary.
def callme(**kwargs):
    return kwargs

In [29]:
print callme(name='kumar',gender='m')
print callme(name='kumar',maiden='vijaya')
print callme(name='kumar',maiden='vijaya',loc='hyd',gender='m')


{'gender': 'm', 'name': 'kumar'}
{'name': 'kumar', 'maiden': 'vijaya'}
{'loc': 'hyd', 'name': 'kumar', 'maiden': 'vijaya', 'gender': 'm'}

In [32]:
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 'loc' in kwargs:
        print "my location is {}".format(kwargs['loc'])
    if 'maiden' in kwargs:
        print "my mother name is {}".format(kwargs['maiden'])

In [33]:
print callme(name='kumar',gender='m')
print callme(name='kumar',maiden='vijaya')
print callme(name='kumar',maiden='vijaya',loc='hyd',gender='m')


my name is kumar
my gender is m
None
my name is kumar
my mother name is vijaya
None
my name is kumar
my gender is m
my location is hyd
my mother name is vijaya
None

In [35]:
# function is a first class object.
# function is just like any other object like string,number and integer.

def my_add(a,b):
    return a + b

def my_sub(c,d):
    return c - d

def my_extra(func,x,y):
    return func(x,y)

In [36]:
print my_extra(my_add,22,33)
print my_extra(my_sub,33,22)


55
11

In [ ]:
# function within a function.
# function closures
# map,filter and lambda

In [ ]:
# function within a function

In [4]:
def outer():
    x = 1        # global variabel for inner and local for outer.
    def inner(): # local function/scope of outer.
        return x
    return inner()

In [6]:
print outer() # 1,no output
print inner() # 1,no output


1
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-b41bfa3ad3a5> in <module>()
      1 print outer() # 1,no output
----> 2 print inner() # 1,no output

NameError: name 'inner' is not defined

In [7]:
# 
def fun():
    pass

print fun
print type(fun)
fun


<function fun at 0x7f96846a6050>
<type 'function'>
Out[7]:
<function __main__.fun>

In [10]:
# function closures : During defination of function,the function address holds all the variable 
# available both locally and globally.
# decorators.

def outer():
    x = 1        # global variabel for inner and local for outer.
    def inner(): # local function/scope of outer.
        return x
    print locals()
    return inner # address of function inner

In [12]:
foo = outer()
print x


{'x': 1, 'inner': <function inner at 0x7f96846a6320>}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-56a6594c95d9> in <module>()
      1 foo = outer()
----> 2 print x
      3 '''
      4     def inner(): # local function/scope of outer.
      5         return x

NameError: name 'x' is not defined

In [13]:
'''
    def inner(): # local function/scope of outer.
        return x
'''
print foo
print foo()


<function inner at 0x7f96846a6320>
1

In [14]:
# map,filter and lambda

In [15]:
# map
print help(map)


Help on built-in function map in module __builtin__:

map(...)
    map(function, sequence[, sequence, ...]) -> list
    
    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

None

In [17]:
def square(a):
    return a * a

# square
print square(2)
print square(3)
print square(4)


4
9
16

In [18]:
print map(square,[1,4,6,8,12,14,17,19])


[1, 16, 36, 64, 144, 196, 289, 361]

In [19]:
# filter
print help(filter)


Help on built-in function filter in module __builtin__:

filter(...)
    filter(function or None, sequence) -> list, tuple, or string
    
    Return those items of sequence for which function(item) is true.  If
    function is None, return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.

None

In [20]:
def even(a):
    if a % 2 == 0:
        return 'even'
    
print even(2) # true
print even(3) # false


even
None

In [21]:
print filter(even,[1,2,3,4,5,6,7,8,9,10])


[2, 4, 6, 8, 10]

In [22]:
# other examples
print map(square,[1,4,6,8,12,14,17,19])    # output after applying sequence to function.
print filter(square,[1,4,6,8,12,14,17,19]) # give you elementf of sequence for function being true.


[1, 16, 36, 64, 144, 196, 289, 361]
[1, 4, 6, 8, 12, 14, 17, 19]

In [23]:
print filter(even,[1,2,3,4,5,6,7,8,9,10])
print map(even,[1,2,3,4,5,6,7,8,9,10])


[2, 4, 6, 8, 10]
[None, 'even', None, 'even', None, 'even', None, 'even', None, 'even']

In [25]:
# lambda
# nameless function.
print map(square,[1,4,6,8,12,14,17,19])
print map(lambda a:a*a,[1,4,6,8,12,14,17,19])


[1, 16, 36, 64, 144, 196, 289, 361]
[1, 16, 36, 64, 144, 196, 289, 361]

In [26]:
print filter(even,[1,2,3,4,5,6,7,8,9,10])
print filter(lambda a: a % 2 == 0,[1,2,3,4,5,6,7,8,9,10])


[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]

In [ ]:
# exercises
# https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt