In [ ]:
# function
# A function is a block of code which can be called n number of times.
#
In [1]:
# function start with a 'def' keyword
# function returns a value
# Your function return a None if there is no value to return.if not it returns a value.
def my_func():
print "hello!! world"
In [4]:
print my_func,type(my_func)
print my_func()
my_func
Out[4]:
In [5]:
# return is not a print value.
# return marks the end of the function.
def my_func():
return "hello!!! world"
In [6]:
print my_func()
In [7]:
# return marks the end of the function.
# how many return values can we have in a function ? n number provided we have conditions.
def my_func():
return "hello!!! world"
print "line1"
print "line2"
print "line3"
In [8]:
print my_func()
In [9]:
def my_func():
print "line1"
print "line2"
print "line3"
return "hello!!! world"
In [10]:
print my_func()
In [14]:
# namespaces or local and global variables
# Any value you define within a function is restricted to the local namespace.
# there is no syntax which can get that value for you.
# locals() - inbuild function.
# value x is only available during the runtime of the function.
def my_func():
x = 10
print locals()
return x
In [15]:
print my_func() # 10
In [13]:
print x # None,x,10,error
In [ ]:
# global variables
# If you local variables are not there your function looks into the global namespaces.
In [1]: y = 10
In [2]: def my_func():
...: print locals()
...: return y
...:
In [3]: print my_func() # 10
{}
10
In [5]: globals()
Out[5]:
{'In': ['',
u'y = 10',
u'def my_func():\n print locals()\n return y',
u'print my_func() # 10',
u'print globals()',
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/Documents/git_repos/python-batches/batch-65'],
'_i': u'print globals()',
'_i1': u'y = 10',
'_i2': u'def my_func():\n print locals()\n return y',
'_i3': u'print my_func() # 10',
'_i4': u'print globals()',
'_i5': u'globals()',
'_ih': ['',
u'y = 10',
u'def my_func():\n print locals()\n return y',
u'print my_func() # 10',
u'print globals()',
u'globals()'],
'_ii': u'print my_func() # 10',
'_iii': u'def my_func():\n print locals()\n return y',
'_oh': {},
'_sh': <module 'IPython.core.shadowns' from '/usr/lib/python2.7/dist-packages/IPython/core/shadowns.pyc'>,
'exit': <IPython.core.autocall.ExitAutocall at 0x7fe679b02550>,
'get_ipython': <bound method TerminalInteractiveShell.get_ipython of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x7fe67a449a10>>,
'my_func': <function __main__.my_func>,
'quit': <IPython.core.autocall.ExitAutocall at 0x7fe679b02550>,
'y': 10}
In [16]:
# local variables are given higher priority then global variable.
y = 10
def my_func():
y = 2
print locals()
return y
print my_func() # 2,2
print y # (2,10),10
In [ ]:
# global keyword
In [17]:
balance = 0
def deposit():
balance = balance + 5000
return balance
def withdraw():
balance = balance - 1000
return balance
In [18]:
print deposit()
In [19]:
# one of crazy solution
def deposit():
balance = 0
balance = balance + 5000
return balance
def withdraw():
balance=0
balance = balance - 1000
return balance
In [20]:
# srikant
# imps
print deposit() # 5000
In [21]:
print withdraw()
In [41]:
# exact solution
balance=0
def deposit():
global balance
print locals()
balance = balance + 5000
return balance
def withdraw():
global balance
print locals()
balance = balance - 1000
return balance
In [42]:
# srikant
# imps
print deposit() # 5000
In [43]:
print withdraw()
In [44]:
# pulling balance from global namespace
print globals()['balance']
In [ ]: