In [10]:
name = 'this is a global name'
def greet(name):
print(name)
# enclosing function
name = 'sammy'
def hello():
print('hello'+name)
hello()
greet(name)
print(name)
this is a global name
hellosammy
this is a global name
In [4]:
len
Out[4]:
<function len>
In [15]:
x = 50
print(locals())
def func():
print(locals())
global x
print('this function is now using the global x!')
print('because of global x is: ', x)
x = 2
print('ran func(), changed global x to',x)
print('before calling func(), x is: ', x)
func()
print('value of x (outside of func()) is: ', x)
{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n print(name)\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", 'len', "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "x = 50\n\ndef fun():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n print(globals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\nprint(locals())\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)"], '_oh': {4: <built-in function len>}, '_dh': ['C:\\Users\\richa\\Portable Apps\\WinPython-64bit-3.6.1.0Qt5\\notebooks\\Python-Bootcamp_my_code'], '_sh': <module 'IPython.core.shadowns' from 'C:\\Users\\richa\\Portable Apps\\WinPython-64bit-3.6.1.0Qt5\\python-3.6.1.amd64\\lib\\site-packages\\IPython\\core\\shadowns.py'>, 'In': ['', "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n print(name)\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", 'len', "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet():\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", "x = 50\n\ndef fun():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n print(globals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", "x = 50\nprint(locals())\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)"], 'Out': {4: <built-in function len>}, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x00000216B15F5860>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x00000216B16FB7F0>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x00000216B16FB7F0>, '_': <built-in function len>, '__': '', '___': '', '_i': "x = 50\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", '_ii': "x = 50\n\ndef func():\n print(globals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", '_iii': "x = 50\n\ndef func():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", '_i1': "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", 'name': 'this is a global name', 'greet': <function greet at 0x00000216B2860EA0>, '_i2': "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n print(name)\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", '_i3': "name = 'this is a global name'\n\ndef greet():\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", '_i4': 'len', '_4': <built-in function len>, '_i5': "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", '_i6': "name = 'this is a global name'\n\ndef greet(name):\n print('name')\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", '_i7': "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", '_i8': "name = 'this is a global name'\n\ndef greet():\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", '_i9': "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet()\nprint(name)", '_i10': "name = 'this is a global name'\n\ndef greet(name):\n print(name)\n # enclosing function\n name = 'sammy'\n \n def hello():\n print('hello'+name)\n \n hello()\ngreet(name)\nprint(name)", '_i11': "x = 50\n\ndef fun():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", 'x': 50, 'fun': <function fun at 0x00000216B28171E0>, '_i12': "x = 50\n\ndef func():\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", 'func': <function func at 0x00000216B28600D0>, '_i13': "x = 50\n\ndef func():\n print(globals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", '_i14': "x = 50\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)", '_i15': "x = 50\nprint(locals())\n\ndef func():\n print(locals())\n global x\n print('this function is now using the global x!')\n print('because of global x is: ', x)\n x = 2\n print('ran func(), changed global x to',x)\n \nprint('before calling func(), x is: ', x)\nfunc()\nprint('value of x (outside of func()) is: ', x)"}
before calling func(), x is: 50
{}
this function is now using the global x!
because of global x is: 50
ran func(), changed global x to 2
value of x (outside of func()) is: 2
In [16]:
radius = 10
def sphere_volume(rads):
return 4/3*3.1415*rads**3
sphere_volume(radius)
Out[16]:
4188.666666666666
In [17]:
sphere_volume = lambda rads: 4/3*3.1415*rads**3
sphere_volume(radius)
Out[17]:
4188.666666666666
In [20]:
# function that checks if a number is in a given range
def ran_check(num,low,high):
if num>low and num<high:
return True
else:
return False
ran_check(3,2,5)
Out[20]:
True
In [27]:
# function that checks if a number is in a given range
cool = lambda num,low,high: num>low and num<high
cool(6,2,5)
Out[27]:
False
In [47]:
s = 'this HAS some Upper and Lower case letters'
l = 0
u = 0
for i in s:
if i.isupper():
u+=1
elif i.islower():
l+=1
else:
pass
print(l)
print(u)
30
5
In [49]:
s = 'Hello Mr. Rogers, how are you this fine Tuesday?'
def up_low(s):
l = 0
u = 0
for i in s:
if i.isupper():
u+=1
elif i.islower():
l+=1
else:
pass
print('Sample String:')
print(s)
print('Expected Output:')
print('No. of Upper case characters:', u)
print('No. of Lower case characters:', l)
up_low(s)
Sample String:
Hello Mr. Rogers, how are you this fine Tuesday?
Expected Output:
No. of Upper case characters: 4
No. of Lower case characters: 33
In [ ]:
Content source: rkburnside/python_development
Similar notebooks: