Aug 17-21, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch2/module1-day3.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab2.pipal.in for this training. Use notebook with name module1-day3.ipynb for today's session.
def add(2, 3): # incorrect
return 2+3
One can not have litterals in function definition
def add("a", "b"): # this string litteral , not allowd
return a + b
def add("a", "b"): # this string litteral , not allowd
return "a" + "b"
def add(a, b):
return a+b
def say_hello(name):
print("Hello", name + "!")
None
x = add(2 , 3) ## I am using litterals while calling
x
y = say_hello("python")
print(y)
Problems
NAV to compute NAV. Using this function compute NAV for tatl assets of 25,00,00,000 and liabilities 30,00,000 for 1000 shares.numeric_value which returns actual numeric value. FOr example a value (1234) should get -1234 as numeric values while "1234.5" will still get value as 1234.5
>>> numeric_value("(123)")
-123.0
>>> numeric_value(123)
123.0
Have a look at this code, what will it print?
def twice(x):
print(2*x)
print(twice(twice(3)))
def NAV(assets, liabilities, shares):
return (assets-liabilities)/shares
assets
liabilities
NAV(250000000, 3000000, 1000)
def foo(x, y, z):
s = x + y + z
m = x*y*z
return s+m
def foo(x, y, z):
s = x + y + z
m = x*y*z
return s+m
def foo(x, y, z):
s = x + y + z
m = x*y*z
return s+m
foo(1, 2, 3)
def foo(x, y, z):
s = x + y + z
m = x*y*z
return s+m
foo(1, 2, 3) # this statement has become part of function foo! because of indentation
int("(123)")
float("(123)")
"a string with - in it".replace("-", "_")
"a string without hyphen".replace("-", "_")
def numeric_value(formattednum):
strnum = formattednum.replace("(", "-").replace(")","")
return float(strnum)
numeric_value("(12232)")
numeric_value("22324")
def twice(x):
print(2*x)
twice(5)
twice(4)
print(twice(twice(3)))
print(twice(twice(3)))
6
print(twice(None))
def twice(x):
return 2*x
twice(twice(3))
twice(twice(twice(6)))
A mathematical function which prints the result but does not return a value, is not usable.
n = 5
n
def power_(x):
return x**n ## not a good practice to use gloabl variables!
def power_(x, n):
return x**n
**Additional guidelines
Pitfalls
x = 10
def foo():
print(x)
foo()
x = 10
def foo(y):
y = 20
foo(x)
print(x)
x = 10
def foo():
x = 20
foo()
print(x)
x = 10
def foo():
x = x + 1
foo()
x = 10
def foo():
y = x + 1
foo()
x = 10
def foo(): # rhis definition
x = x + 1
#foo() # this calling the function fooP
x = 10
def foo(): # rhis definition
x = x + 1
foo() # this calling the function foo
[1, 1, 1] + [2]
x = [1, 1, 1]
def appendzero(y): # <----------here x and y are pointing to same list
y = y + [0] # we changed reference of y to somthing new
appendzero(x)
print(x)
statement name memory
x = [1, 1, 1] x
def appendz(y) \
y = y + [0] \
appendz(x) \
appendz->code
\
\ [1, 1, 1]
statement name memory
y
\
y = y + [0] \
\
\
[1, 1, 1, 0]
x = [1, 1, 1]
def appendzero(y):
y.append(0)
appendzero(x)
print(x)
x = [1, 2, 3]
y = x
y = [2,3, 3]
def compound_interest(P, r, n, t):
return P*(1+r/n)**(n*t)
compound_interest(25000, 0.04, 4, 5)
compound_interest(0.04, 25000, 4, 5)
Python has a simple solution for it , called as named argument
def compound_interest(principle, rate, comp_freq, years):
return principle*(1+rate/comp_freq)**(comp_freq*years)
compound_interest(rate=0.04, comp_freq=4, years=5, principle=25000)
compound_interest(rate=0.04, principle= 25000, comp_freq=4, years=5)
compound_interest(25000, comp_freq=4, years=5, rate=0.04)
def compound_interest(principle, years, rate=0.04, comp_freq=4):
return principle*(1+rate/comp_freq)**(comp_freq*years)
compound_interest(25000, 5)
compound_interest(25000, 5, rate=0.07)
x = 10
def foo():
global x
x = 20
foo()
print(x)
x = [1, 1, 1]
def appendzero(y):
y.append(0) ## here we are able to modify global y because it has got a method in it to modify itself.
appendzero(x)
x = [1, 1, 1]
def appendzero():
global x
x = x + [0]
appendzero()
print(x)
x
del x
x
del foo
foo
def foo():
print("Hello foobar!")
foo # a variable with name as function name is created
foo()
x = 10
y = x
bar = foo # aliasing
bar
foo
foo()
bar()
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(5, 6, square)
sumof(5, 6, cube)
sumofsquares(5, 6)
sumofcubes(5, 6)
help(max)
words = ["one", "twelve", "three", "four", "sixty five"]
max(words) ##ASCII order
max(words, key=len)
words = ["Z", "YY", "XXX", "MMMM", "AAAAAAA"]
max(words)
max(words, key=len) # a word with max length
min(words)
min(words, key=len) # a word with minimu length
records = [
("rupali", 21, 9.5),
("rupa", 20, 9.8),
("alice", 16, 9.6),
("elsa", 23, 9.2)
]
max(records) # by name ASCII order
def get_age(row):
return row[1]
def get_score(row):
return row[2]
row = ("rupali", 21, 9.5)
row[0] # name
row[1] # age
row[2] #score
max(records, key=get_age)
max(records, key=get_score)
min(records, key=get_age)
sorted(records, key=get_age)
sorted(records, key=get_score)
sorted(records, key=get_age, reverse=True)
sorted(records, key=get_age)
def make_adder(x):
def adder(y):
return x+y
return adder
adder5 = make_adder(5)
adder5
adder5(7)
adder5(11)
adder5(15)
adder9 = make_adder(9)
adder9
adder9(5)
def get_age(r):
return r[1]
get_age = lambda r : r[1]
max(records, key=lambda r:r[1])
def get_age(r):
print("*"*5, r)
return r[1]
max(records, key=get_age)