Dec 07-11, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch3/module1-day4.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab.pipal.in for this training. Create a notebook with name module1-day4.ipynb for today's session
Problems
>>> NAV(assets,liabilities,shares)
>>> numeric_value("(35.5)")
-35.5
>>> numeric_value("32.5")
32.5
compounded_total which takes P, n, r, and t as arguments of a function and returns total value after t years.x = 10
def foo():
x = x+1
foo()
print(x)
def say_hello(greeting, name):# this function takes two arguments(python programming term) /parameter (english/mathematic)
print(greeting, name, "!")
say_hello("Hello", "Vikrant")
say_hello("Welcome", "Vikrant")# when we call a function , there is new namespace comes into picture
x
hello
def hello(): # it takes no arguments
print("Hello!")
hello # this has to be created?
hello # show me what is this object which has name hello!
hello() # this is calling the fuction with no argument...
def f(a): # the momemnt we execute this defination of function, a variable with name `f` (function name)
return a*a+2
f # not calling function, this is just examining the obejct woth name f!
print(f)
f(4) ### it automatically creates a variable of name a (name of paramater)
f(7)
x = f(7) # i will get value returned f into x
def NAV(assets, liabilities, shares):
return (assets - liabilities)/shares
def numeric_value(strvalue):
v = strvalue.replace("(","-").replace(")","")
return float(v)
numeric_value("(35.6)")
numeric_value("56")
"(35)".replace("(","-").replace(")","") # string
float("(35)".replace("(","-").replace(")",""))
def add(a, b):
return a+b
x, y = 5, 6
add(x,y) # this will make x-> a , y->b inside function
add(8, 10) # a and b are defined as 8, 10 ...which only in the function
def foo():
print(x) # x is not there! if it is not defined locally in function it will take the value from namespace of caller
x
x
foo()
x = 10 # global namespace x = 10
def foo(): # foo namespace does not have x
x = 20 # foo (local to foo) namespace ... it creates a new name x in foo namespace and assignes a value of 20
foo()
# the moment foo call is over, namespace foo ... is gone!
print(x) # global name space x => 10
x = 10
def foo(x):
x = 20 # the assignment operator ... whatever is left hand side it create a name for that in current name space
return x
s = foo(x)
print(x)
def appendzero(x):
x.append(0) # method does the modfication of the object
nums = [1, 1, 1]
appendzero(nums)
nums
x = 10
l = [1, 2, 3, 4]
#l = [1, 2, 3, 4, 0] # this will create new list
l.append(0)
3*4 # repr
add(3, 4) # repr
x = add(3, 4)
hello = "hello"
hello.upper()
def square(x):
return x*x
square
x = 10
y = x # aliasing
y
x
sqr = square
sqr
square
square(5)
sqr(5)
add(x, y)
def square(x):
return x*x
def cube(x):
return x*x*x
def sumofsquares(x, y):
return square(x) + square(y)
def sumofcubes(x, y):
return cube(x) + cube(y)
def sumof(x, y, func):
return func(x) + func(y)
sumof(9 , 18, square)
sumofsquares(9, 18)
sumof(9, 18, cube)
abs(-5)
sumof(-5, 10, abs)
words = ["one", "two", "three", "four", "five", "six"]
max([1, 2, 45, 2, 5])
max(words) # finding max by dictionary position, it tries to arrange alphabeticaly
max(words, key=len)
sorted(words)
sorted(words, key=len)
max(["hello", "text", "random", "words"]) #arrange it alphabetical (ASCII) order and return last one
max(["hello", "text", "random", "words"], key=len) #len("string")
records = [
("TATA", 200.0, 5.5),
("REL",2000.5, -5),
("INF", 1550.3, 3.0),
("HCL",1200, 70.5)
]
records[0]
max(records)
x = ("TATA", 200.0, 5.5)
x[1]
x[0]
x[2]
def get_value(r):
return r[1]
def get_name(r):
return r[0]
def get_gain(r):
return r[2]
max(records, key=get_value)
max(records, key=get_gain)
min(records, key=get_value)
min(records, key=get_gain)
sorted(records, key=get_name)
sorted(records, key=get_value)
sorted(records, key=get_gain)
x = 10
def foo():
x = x+1 # the assignment operator delinks my name x from global, it create a local
# you can't do modificaton of global like this
foo()
print(x)
x = 10
def foo():
print(x) # take from global context on for reading
foo()
print(x)
doom
doom = doom + 4
x = 10
def foo():
y = x+1
foo()
print(x)
True
False
"Vikrant".startswith("V") # boolean value
"hel" in "Hello"
"hel" in "hello"
"hello" in "Worlds"
x = "hello"
y = "hello"
x == y # check if x and y are same
x in y # check if x in y
"one" in ["one", "two", "three", "four"]
"name" in {"name":"Alice", "age":20} # check if 'name' is in keys of dictionary
#x in collection # check if x is there in collection
x, y = 5, 4
x > y
x<y
x <= y
y >= x
x != y # results in True if x and y are not equal
if "hel" in "cell":
x = 30
y = x+45
print("hell!") # exeute this if condition if this block is True
if "hel" in "cell":
x = 30
y = x+45
print("hell!") # exeute this if condition if this block is True
elif "cel" in "hell":
print("cell!")
elif "del" in "bell":
print("dell!")
else:
print("None of the conditons were True!")
cond = True
if cond:
x = 2
else:
x = 3
x
def get_value_x(cond):
if cond:
return 2
else:
return 3
get_value_x(True)
min
max
def min_(x, y):
if x < y:
return x
else:
return y
min_(45, 23)
def print_list(items):
i = 0
while i < len(items):# till this condition is True, execute following block
print(items[i])# prints one line at a time
i = i +1
numbers = [3, 2, 1, 3, 6]
print_list(numbers)
numbers[0] # numbers[i] i = 0
numbers[1]# numbers[i] i = 1
numbers[2]# numbers[i] i = 2
numbers[3]# numbers[i] i = 3
numbers[4]# # numbers[i] i = 4
numbers[5]# # numbers[i] i = 5 # this cond has to be avoided
i == 5 # this is to be avoided
i < 5 # is allowed
def print_list(items):
i = 0
while i < len(items):# till this condition is True, execute following block
print(items[i])# prints one line at a time
i = i +1
i = 0
i < len(numbers)
#print(numbers[i])# prints one line at a time
i = i +1
i
i < len(numbers)
#print(numbers[i])# prints one line at a time
i = i +1
i
i < len(numbers)
#print(numbers[i])# prints one line at a time
i = i +1
i < len(numbers)
i
#print(numbers[i])# prints one line at a time
i = i +1
i
i < len(numbers)
#print(numbers[i])# prints one line at a time
i = i +1
i
i < len(numbers)
print_list(numbers)
def print_list(items):
i = 0
while i < len(items):# till this condition is True, execute following block
i = i + 1
print(items[i])# prints one line at a time
print_list(numbers)
numbers
def print_list(items):
i = 0
while i < len(items):# till this condition is True, execute following block
print(items[i])# prints one line at a
i = i + 1 # do this at end of loop
print_list(numbers)
def print_list1(items):# not recommanded
i = -1
while i < (len(items)-1):# till this condition is True, execute following block
i = i + 1
print(items[i])# prints one line at a
print_list1(numbers)
def print_list(items):
i = 0
while i < len(items):# till this condition is True, execute following block
x = items(i)#<--------
print(x)# prints one line at a
i = i + 1 # do this at end of loop
print_list(numbers)
name = "Vikrant"
name(0)
name[0]# any access of items from collection is using square backet
def print_list_single_line(items):
i = 0
while i < len(items):
print(items[i], end=",") # tell print not to end every print with \n
i = i + 1
print_list_single_line(numbers)
def print_list_single_line(items, sep):
i = 0
while i < len(items):
print(items[i], end=sep) # tell print not to end every print with \n
i = i + 1
print_list_single_line(numbers, " ")
print_list_single_line(numbers, ",")
print_list_single_line(numbers, "")
print_list_single_line(numbers, "\n")
def print_list_single_line(items, sep=","): # default argument
i = 0
while i < len(items):
print(items[i], end=sep) # tell print not to end every print with \n
i = i + 1
print_list_single_line(numbers) # sep is taken as default value
print_list_single_line(numbers, ":")
def addone(a, b=1):
return a+b
addone(3)
addone(3, 5)
def addone_(a, b):
return a + b
addone_(5)
def addone(b=1, a):
return a+b
def addone(a, b=1):
return a +b
words = ["one", "two", "three", "four"]
for word in words:
print(word, end=",")
def printlist(items):
for item in items:
print(item)
printlist(numbers)
students = ["A","B","C","D","E"]
for student in students:
print(student, end=",")
def sumlist(items):
s = 0
for n in items:
s = s + n
return s
sumlist(numbers)
sumlist(words)
def sumlist(items, start=0):
s = start
for n in items:
s = s + n
return s
sumlist(numbers)
sumlist(words, "")
for c in "string":
print(c, end=",")
for key in {"x":1,"y":2}:
print(key)
for s in (1, 2, 3,4, 5):
print(s, end=",")
>>> product([3, 2, 4])
24
>>> factorial(5)
120
>>> findlens(["one", "two", "three"])
[3, 3, 5]
>>> find_words_of_len(words, 3)
['one', 'two', 'six']
>>> unique([1, 1, 2, 3, 1, 2, 3, 2, 4])
[1, 2, 3, 4]
urls = ['www.abrakadabra.com/dccEcB/EGdd',
'www.abrakadabra.com/gADFeD/bcAF',
'www.abra.com/AGadbb/eagE',
'www.dabra.com/cffdfD/FCAD',
'www.abra.com/GFGaBE/dcfc',
'www.abra.com/gaFegG/Bdaf',
'www.abrakadabra.com/aGabaf/EEfa',
'www.dabra.com/ceEgFD/bGgc',
'www.dabra.com/bDEffC/bcEA']
Write a function min2 which find minimum from given two numbers. Also write a function min3 which can find minimum number from given 3 numbers. Do not make use of bulit in min function.
Write a function rearramge_max to rearrange digits of an integer so as to make maximum integer from it.
>>> rearramge_max(1312)
3211