Sep 13-17, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch1/module1-day3.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from https://lab.pipal.in for this training.
login to hub and create a notebook with name module1-day3
>>> NAV(assets,liabilities,shares)
>>> numeric_value("(35.5)")
-35.5
>>> numeric_value("32.5")
32.5
def NAV(assets, liabilities, shares):
return (assets-liabilities)/shares
def NAV_(assets, liabilities, shares):
nav = (assets-liabilities)/shares
print(nav)
NAV(250000000, 3000000, 1000)
247000.0
NAV_(250000000, 3000000, 1000)
247000.0
x = NAV(250000000, 3000000, 1000)
y = NAV_(250000000, 3000000, 1000)
247000.0
print(x, y)
247000.0 None
def twice(x):
return 2*x
def twice_(x):
"""This function returns None!
"""
print(2*x)
twice(twice(5))
20
twice_(twice_(5))
10
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-16-117f8e4344b4> in <module> ----> 1 twice_(twice_(5)) <ipython-input-15-381d964473c3> in twice_(x) 6 """This function returns None! 7 """ ----> 8 print(2*x) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
2*None
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-ebf0962da868> in <module> ----> 1 2*None TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
def foo(x):
if x:
return 1
else:
return 0
def bar(x):
return x
print("Hello") # execution never reaches here! because after return is executed function call is over!
return x*x
bar(5)
5
def foobar(x):
print(x)
print(x*x)
foobar(5)
5 25
def numeric_value(strvalue):
return strvalue.replace("(", "-").replace(")","")
def delete(source_text, to_deleted):
return source_text.replace(to_deleted, "")
delete("delete delete from this statement", "delete")
' from this statement'
numeric_value("(1234)")
'-1234'
def numeric_value(strvalue):
"""This function works on text
"""
modified = strvalue.replace("(", "-").replace(")","")
return float(modified)
numeric_value("12343")
12343.0
numeric_value("(565.5)")
-565.5
numeric_value((10))
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-35-9748b590e9bd> in <module> ----> 1 numeric_value((10)) <ipython-input-32-8b5d1b4283f8> in numeric_value(strvalue) 1 def numeric_value(strvalue): ----> 2 modified = strvalue.replace("(", "-").replace(")","") 3 return float(modified) AttributeError: 'int' object has no attribute 'replace'
numeric_value("(10)")
-10.0
value = input("Enter your value to be converted:")
numeric_value(value)
-43434.0
numeric_value("12343") # I am calling function with parameter which is litteral string
12343.0
strv = "4356354"
numeric_value(strv) # here the function is gettting called with paramter which is a variable which contains a string
4356354.0
def drop(items, n):
return items[n:] # list slicing
drop([1,2,3,4,5,6,7,8,9], 3)
[4, 5, 6, 7, 8, 9]
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
x = 4
drop(numbers, x)
[5, 6, 7, 8]
Some guidelines to remember
style guide
f(x) = x*x + 2*x = 1
def square(a):
return a*a
def square_(a):
print(a*a)
def double(a):
return 2*a
def double_(a):
print(2*a)
def compute_poly(x):
return square(x) + twice(x) + 1
def compute_poly_(x):
return square_(x) + twice_(x) + 1
compute_poly(4)
25
compute_poly_(4)
16 8
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-49-f407d6f48f8f> in <module> ----> 1 compute_poly_(4) <ipython-input-47-7c270fc97549> in compute_poly_(x) 16 17 def compute_poly_(x): ---> 18 return square_(x) + twice_(x) + 1 TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
def cylinder_volume(radius, height):
return 3.14*radius**2*height
cylinder_volume(1, 2)
6.28
cylinder_volume(2, 1) # by mistake height is paased first , radius is passed later...retsults will be wrong
12.56
drop(3, [1, 2, 3, 4,5]) # easy to catch
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-55-67846ef1a876> in <module> ----> 1 drop(3, [1, 2, 3, 4,5]) # easy to catch <ipython-input-43-0effee6139ea> in drop(items, n) 1 def drop(items, n): ----> 2 return items[n:] # list slicing TypeError: 'int' object is not subscriptable
cylinder_volume(radius=1, height=2) # this is named parameter passing
6.28
cylinder_volume(height=2, radius=1)
6.28
def cylinder_volume1(height, radius=1):
"""if user does not pass raiduis parameter, it will be taken as 1 else whatever user passes
"""
return 3.14*radius**2*height
cylinder_volume1(2) # only height is passed, radius is taken as default 1
6.28
cylinder_volume1(2, 2) # positional arguments
25.12
cylinder_volume1(radius=3, height=1)
28.26
help(cylinder_volume1)
Help on function cylinder_volume1 in module __main__:
cylinder_volume1(height, radius=1)
if user does not pass raiduis parameter, it will be taken as 1 else whatever user passes
def square(x):
"""This a function to compute square
it takes one argument
which is called x here
which can be float or integer
"""
return x**2
help(square)
Help on function square in module __main__:
square(x)
This a function to compute square
it takes one argument
which is called x here
which can be float or integer
porblems
What will this print?
x = 10
def foo(x):
x = 30
foo()
print(x)
x = [1, 2, 3, 4]
x = [ 1, 1, 1]
x = 10
def foo():
x = 30
foo()
print(x)
10
del x
def foo():
x = 30 # assignment ... which is like write
foo()
print(x)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-74-b066cf69ac6d> in <module> ----> 1 del x 2 3 def foo(): 4 x = 30 # assignment ... which is like write 5 NameError: name 'x' is not defined
x = 10
def foo():
print(x) # reading! if it is not there in local namesapce, you can access from global for reading only
foo()
del x
10
x = 10
def foo():
x = x + 1 # this is trying to read as well as trying to create a new local variable
foo()
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-80-cee6ef707259> in <module> 4 x = x + 1 5 ----> 6 foo() <ipython-input-80-cee6ef707259> in foo() 2 3 def foo(): ----> 4 x = x + 1 5 6 foo() UnboundLocalError: local variable 'x' referenced before assignment
x = 10
def foo():
global x # don't make use of this unless you understand it completely.
x = x + 1 # this is trying to read as well as trying to create a new local variable
foo()
print(x)
del x
11
11
numbers = [1, 1, 1, 1]
def appendzero(items):
items.append(0)
appendzero(numbers)
print(numbers)
[1, 1, 1, 1, 0]
statement namespcae memory statement namespace memeory
numbers = [1, 1, 1] numbers items (will point to numbers)
appendzero(numbers) [1, 1, 1, 1]
numbers = [1, 1, 1, 1]
def appendzero(items):
items.append(0)
appendzero(numbers)
print(numbers)
[1, 1, 1, 1, 0]
numbers = [1, 1, 1, 1]
def foobar(items):
items = [1, 1] # assignment operator will remove global link of paramter...and create a local
foobar(numbers)
print(numbers)
[1, 1, 1, 1]
numbers = [1, 1, 1, 1]
def foobar(numbers):# a useless function
numbers = [1, 1] # assignment operator will remove global link of paramter...and create a local
foobar(numbers)
print(numbers)
[1, 1, 1, 1]
numbers = [1, 1, 1, 1]
def foobar(numbers):#
numbers = [1, 1] # assignment operator will remove global link of paramter...and create a local
return numbers
x = foobar(numbers)
print(numbers) # still global
print(x) # result of fucntion
[1, 1, 1, 1] [1, 1]
def hello_world():
print("Hello World!")
print(hello_world)
<function hello_world at 0x7fb5ab3198b0>
def square(x):
return x*x
def sumofsquares(x, y):
return square(x) + square(y)
def cube(x):
return x**3
def sumofcubes(x, y):
return cube(x) + cube(y)
def sumof(x, y, func):
return func(x) + func(y)
sumof(3, 4, square)
25
sumof(3, 4, cube)
91
import math
sumof(3, 2, math.sin)
1.0504174348855488
words = ['one', 'two', 'three', 'four', 'five']
max(words) # will return last item in dictionary
'two'
max(words, key=len)
'three'
income_sources = {
"stocks" : 560000,
"salary" : 600000,
"rent" : 200000
}
max(income_sources) # will work on keys!
'stocks'
def value(key):
return income_sources[key]
max(income_sources, key=value)
'salary'
records = [
("TATA", 200.0, 5.5),
("INFY", 2000.5, -5),
("REL", 1500.3, 50.0),
("HCL", 1100.4, 70.5)
]
max(records)
('TATA', 200.0, 5.5)
records[0]
('TATA', 200.0, 5.5)
records[1]
('INFY', 2000.5, -5)
item = records[1]
item[2]
-5
def get_value(item):
return item[1] # get value , i.e. item of index 1
def get_gain(item):
return item[2]
max(records, key=get_value)
('INFY', 2000.5, -5)
records
[('TATA', 200.0, 5.5),
('INFY', 2000.5, -5),
('REL', 1500.3, 50.0),
('HCL', 1100.4, 70.5)]
max(records, key=get_gain)
('HCL', 1100.4, 70.5)
``` flow of max execution
max(records, get_gain)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-120-4c43c25e1c32> in <module> ----> 1 max(records, get_gain) TypeError: '>' not supported between instances of 'function' and 'list'
max(2, 3)
3
help(max)
Help on built-in function max in module builtins:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
records = [("TATA", 200.0, 5.5),("INFY", 2000.5, -5),("REL", 1500.3, 50.0),("HCL", 1100.4, 70.5)]
stocks = {
"name": "IBM",
"value": 130,
"open": 131
}
"""this is just
to make
things
more redable! when you look at python
code!!
"""
'this is just\nto make\nthings \nmore redable! when you look at python \ncode!!\n'
records
[('TATA', 200.0, 5.5),
('INFY', 2000.5, -5),
('REL', 1500.3, 50.0),
('HCL', 1100.4, 70.5)]
def get_gain_(item):
print("This is the argument that I got!", item)
return item[2]
max(records, key=get_gain_)
This is the argument that I got! ('TATA', 200.0, 5.5)
This is the argument that I got! ('INFY', 2000.5, -5)
This is the argument that I got! ('REL', 1500.3, 50.0)
This is the argument that I got! ('HCL', 1100.4, 70.5)
('HCL', 1100.4, 70.5)
min(records, key=get_gain_)
This is the argument that I got! ('TATA', 200.0, 5.5)
This is the argument that I got! ('INFY', 2000.5, -5)
This is the argument that I got! ('REL', 1500.3, 50.0)
This is the argument that I got! ('HCL', 1100.4, 70.5)
('INFY', 2000.5, -5)
sorted(records, key=get_gain_)
This is the argument that I got! ('TATA', 200.0, 5.5)
This is the argument that I got! ('INFY', 2000.5, -5)
This is the argument that I got! ('REL', 1500.3, 50.0)
This is the argument that I got! ('HCL', 1100.4, 70.5)
[('INFY', 2000.5, -5),
('TATA', 200.0, 5.5),
('REL', 1500.3, 50.0),
('HCL', 1100.4, 70.5)]
sorted(records, key=get_gain_, reverse=True)
This is the argument that I got! ('TATA', 200.0, 5.5)
This is the argument that I got! ('INFY', 2000.5, -5)
This is the argument that I got! ('REL', 1500.3, 50.0)
This is the argument that I got! ('HCL', 1100.4, 70.5)
[('HCL', 1100.4, 70.5),
('REL', 1500.3, 50.0),
('TATA', 200.0, 5.5),
('INFY', 2000.5, -5)]