Oct 11-15, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch2/
© Pipal Academy LLP
We will be using jupyter hub from https://lab2.pipal.in for this training.
create a notebook with name module2-day1
items = ["col1,col2,col2",
"2,2,1,",
"1,1,1,13",
"1,1,31",
"1,1,1,1,1,1,1343"]
max(items)
'col1,col2,col2'
def get_fields_count(line):
fields = line.split(",") # split the the text data based on comma and make a list
return len(fields)
max(items, key=get_fields_count)
'1,1,1,1,1,1,1343'
volume = ["1.2B",
"2.3M",
"3.4B",
"1.2M",
"0"]
def get_numeric_volume(v):
if v.endswith("B"):
return float(v[:-1])*10**9
elif v.endswith("M"):
return float(v[:-1])*10**6
else:
return float(v)
max(volume, key=get_numeric_volume)
'3.4B'
min(volume, key=get_numeric_volume)
'0'
sorted(volume, key=get_numeric_volume)
['0', '1.2M', '2.3M', '1.2B', '3.4B']
%%file add.py
def add(a, b):
return a+b
Overwriting add.py
import add # module
add.add(3, 4)
7
%%file addapp.py
import sys
def add(x, y):
return x+y
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a,b))
Overwriting addapp.py
!python3 addapp.py 34 67
101
items
['col1,col2,col2', '2,2,1,', '1,1,1,13', '1,1,31', '1,1,1,1,1,1,1343']
for line in items: # first pattern
print(line)
col1,col2,col2 2,2,1, 1,1,1,13 1,1,31 1,1,1,1,1,1,1343
for i in range(5):
print(i)
0 1 2 3 4
for i in reversed(range(5)):
print(i)
4 3 2 1 0
items
['col1,col2,col2', '2,2,1,', '1,1,1,13', '1,1,31', '1,1,1,1,1,1,1343']
for line in reversed(items):
print(line)
1,1,1,1,1,1,1343 1,1,31 1,1,1,13 2,2,1, col1,col2,col2
nums = [1, 2, 3, 4, 5]
for n in reversed(nums):
print(5)
5 5 5 5 5
nums
[1, 2, 3, 4, 5]
print(reversed(nums))
<list_reverseiterator object at 0x7f5a2eab0d90>
colors = ("red", "green", "blue")
for c in reversed(colors):
print(c)
blue green red
for char in reversed("this is text data"):
print(char, end=",") # print by defaults adds new lineafter every print! we are telling it that instead print ,
a,t,a,d, ,t,x,e,t, ,s,i, ,s,i,h,t,
poem = """This is first line of my poem
and this is another
which is obvously fake poem
but I don't mind it
calling as a poem"""
poem.split() # words!
['This', 'is', 'first', 'line', 'of', 'my', 'poem', 'and', 'this', 'is', 'another', 'which', 'is', 'obvously', 'fake', 'poem', 'but', 'I', "don't", 'mind', 'it', 'calling', 'as', 'a', 'poem']
poem.split("\n")
['This is first line of my poem', 'and this is another', 'which is obvously fake poem', "but I don't mind it", 'calling as a poem']
lines = poem.split("\n")
for i, line in enumerate(lines):
print(i, line)
0 This is first line of my poem 1 and this is another 2 which is obvously fake poem 3 but I don't mind it 4 calling as a poem
for i, line in enumerate(lines, start=1):
print(i, line)
1 This is first line of my poem 2 and this is another 3 which is obvously fake poem 4 but I don't mind it 5 calling as a poem
firstnames = ["Alice","Elsa", "Alex", "Elisa", "Vikrant"]
lastnames = ["Wondergirl", "Frozen", "Lion", "Hacker", "Patil"]
for name, surname in zip(firstnames, lastnames):
print(name, surname)
Alice Wondergirl Elsa Frozen Alex Lion Elisa Hacker Vikrant Patil
xs = ["x1","x2","x3"]
ys = ["y1","y2","y3","y4"]
for x,y in zip(xs, ys):
print(x, y)
x1 y1 x2 y2 x3 y3
xs = ["x1","x2","x3"]
ys = ["y1","y2","y3","y4"]
zs = ["z1","z2","z3","z4"]
for x,y,z in zip(xs, ys, zs):
print(x, y, z)
x1 y1 z1 x2 y2 z2 x3 y3 z3
nums
[1, 2, 3, 4, 5]
r = reversed(nums) # this is iterator and not a list
for item in r: # this can be done only once
print(item)
5 4 3 2 1
for item in r: # r is already consumed in about for loop!
print(item) # here this loop will be empty
for i in reversed(range(5)):
print(i)
4 3 2 1 0
nums = [1, 2, 3, 4, 5]
r = reversed(nums)
for item in r: # this can be done only once
print(r)
<list_reverseiterator object at 0x7f34d3533190> <list_reverseiterator object at 0x7f34d3533190> <list_reverseiterator object at 0x7f34d3533190> <list_reverseiterator object at 0x7f34d3533190> <list_reverseiterator object at 0x7f34d3533190>
nums = [1, 2, 3, 4, 5]
r_nums = reversed(nums)
for i in r_nums: # this can be done only once
print(i)
5 4 3 2 1
Problems
Write a function called vector_add, which does vector addition (component wise addition)
>>> v1 = [1, 1, 1]
>>> v2 = [3, 4, 5]
>>> vector_add(v1, v2)
[4, 5, 6]
A matrix is called as unit matrix if diagnal elements are nothing but 1. and all off diagonal elements are zero. Write a function is_unit_matrix
>>> m = [[1, 0 , 0],
[0, 1, 0],
[0, 0, 1]]
>>> is_unit_matrix(m)
True
Write a function called invertprint which takes a poem as multiline text and prints in order such that last line is first ..and first line at last
>>> poem = """This is first line of my poem
and this is another
which is obvously fake poem
but I don't mind it
calling as a poem"""
>>> invertprint(poem)
calling as a poem
but I don't mind it
which is obviously fake poem
and this is another
This is first line of my poem
v1 = [1, 2, 3]
v2 = [2, 3, 4]
v1 + v2 # addition for a list is nothing but concatenation
[1, 2, 3, 2, 3, 4]
zip(v1, v2)
<zip at 0x7f34d30a3c00>
zip(v1+v2)
<zip at 0x7f34d30a5900>
def vector_add(v1, v2):
v3 = []
for x,y in zip(v1, v2):
v3.append(x+y)
return v3
def vector_add(v1, v2):
i = 0
v3 = []
for item in v1:
v4 = v1[i] + v2[i]
v3.append(v4)
i = i + 1
return v3
def vector_add(vector1, vector2):
v = []
for v1, v2 in zip(vector1, vector2):
v4 = v1 + v2
v.append(v4)
return v
def vector_add(v1, v2):
v3 = []
for i, item in enumerate(v1):
v4 = item + v2[i]
v3.append(v4)
return v3
vector1 = [1, 2, 3]
vector2 = [2, 3, 4]
vector3 = [2, 3, 4]
vector4 = [1, 2, 3]
for x, y, z, m in zip(vector1, vector2, vector3, vector4):
def add(x, y):
return x+y
add(3, 4)
7
add(3, 5, 6)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-7ab0921c8354> in <module> ----> 1 add(3, 5, 6) TypeError: add() takes 2 positional arguments but 3 were given
def myadd(*args):
print(args)
myadd(2, 3)
(2, 3)
myadd(3, 4, 5)
(3, 4, 5)
def myadd(*args):
s = 0
for i in args:
s += i
return s
myadd(1, 2, 3, 5)
11
myadd(1, 2)
3
nums = [1, 2, 3, 4, 6, 7, 8, 8]
#myadd(nums[0], nums[1], )...
myadd(*nums)
39
vector1 = [1, 2, 3]
vector2 = [2, 3, 4]
vector3 = [2, 3, 4]
vector4 = [1, 2, 3]
vector5 = [1, 1, 1]
list_of_vectors = [vector1,
vector2,
vector3,
vector4,
vector5]
v = []
for components in zip(*list_of_vectors):
v.append(sum(components))
print(v)
[7, 11, 15]
vector1 = [1, 0, 0]
vector2 = [0, 3, 0] # this is not allowed
vector3 = [4, 0, 0]
vector4 = [1, 2, 2]
vector5 = [1, 1, 1]
csvfile = """1,1,2
,1,
,,3
1,2,3
"""
for line in csvfile.split("\n"):
print(line)
1,1,2 ,1, ,,3 1,2,3
for line in csvfile.split("\n"):
print(line.split(","))
['1', '1', '2'] ['', '1', ''] ['', '', '3'] ['1', '2', '3'] ['']
def handle_spaces(line):
values = line.split(",")
intvalues = []
for v in values:
if v == "":
intvalues.append(0)
else:
intvalues.append(int(v))
return intvalues
def populate_spaces_with_zeros(csvfiledata):
finaldata = []
for line in csvfiledata.split("\n"):
intvalues = handle_spaces(line)
finaldata.append(intvalues)
return finaldata
populate_spaces_with_zeros(csvfile)
[[1, 1, 2], [0, 1, 0], [0, 0, 3], [1, 2, 3], [0]]
m = [[1, 0 , 0],
[0, 1, 0],
[0, 0, 1]]
def is_unit_matrix(matrix):
for i, row in enumerate(matrix): # i is row index
if row[i] != 1: # diagonal element
return False
for j, item in enumerate(row): # j is column index
if i!=j and item!=0:
return False
return True
is_unit_matrix(m)
True
m1 = [[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
is_unit_matrix(m1)
False
def invertprint(data):
for line in reversed(data.split("\n")):
print(line)
invertprint(poem)
calling as a poem but I don't mind it which is obvously fake poem and this is another This is first line of my poem
def findlens(words):
lens = []
for word in words:
lens.append(len(word))
return lens
def squares(nums):
sqrs = []
for n in nums:
sqrs.append(n*n)
return sqrs
import datetime
def trange(n):
"""generates next n dates startng from today
"""
dates = []
start = datetime.datetime.today()
for i in range(n):
nextdate = start + datetime.timedelta(days=i)
dates.append(nextdate)
return dates
findlens(["one", "twi", "three"])
[3, 3, 5]
squares([2, 3, 4, 4])
[4, 9, 16, 16]
new_list = []
for item in in old_list:
newitem = do_some_operation(item)
new_list.append(newitem)
[ do_some_operation(item) for item in old_list]
def findlens(words):
lens = []
for word in words:
lens.append(len(word))
return lens
words = "Hello these are some words".split()
[len(word) for word in words]
[5, 5, 3, 4, 5]
def squares(nums):
sqrs = []
for n in nums:
sqrs.append(n*n)
return sqrs
[n*n for n in nums]
[1, 4, 9, 16, 36, 49, 64, 64]
import datetime
def trange(n):
"""generates next n dates startng from today
"""
dates = []
start = datetime.datetime.today()
for i in range(n):
nextdate = start + datetime.timedelta(days=i)
dates.append(nextdate)
return dates
[datetime.datetime.today() + datetime.timedelta(days=i) for i in range(5)]
[datetime.datetime(2021, 10, 11, 12, 24, 5, 129980), datetime.datetime(2021, 10, 12, 12, 24, 5, 130003), datetime.datetime(2021, 10, 13, 12, 24, 5, 130008), datetime.datetime(2021, 10, 14, 12, 24, 5, 130012), datetime.datetime(2021, 10, 15, 12, 24, 5, 130014)]
def evens(nums):
e = []
for n in nums:
if n%2==0:
e.append(n)
return e
evens([1, 1, 2, 3, 1, 3, 4, 4, 6, 8])
[2, 4, 4, 6, 8]
nums = [1, 1, 2, 3, 1, 3, 4, 4, 6, 8]
[n for n in nums if n%2==0]
[2, 4, 4, 6, 8]
[n*n for n in nums if n%2==0]
[4, 16, 16, 36, 64]
problems
Write a function listpy which works eactly like lisdir ..but it give back list of only python files( with extention ".py"
>>> import os
>>> listpy(os.getcwd())
['add.py','square.py', ...]
Find sum of all multiples of 11 or 13 below 1000
factors which finds all fatctors of given number (1 and self included)is_prime which tells if given number is prime number or not.import os
os.listdir()
['index.html', 'module1-day5.html', 'mymodule.py', 'push', 'module1-day5.ipynb', 'square.py', '__pycache__', 'Untitled.html', 'index.ipynb', 'module1-day1.html', 'module1-day1.ipynb', 'module1-day2.ipynb', 'add.py', 'module1-assignment.ipynb', 'aad.py', 'module2-day1.ipynb', 'module1-day3.html', 'module1-day2.html', 'module1-day3.ipynb', 'module1-assignment.html', 'module1-day4.html', 'Makefile', 'module2-day1.html', '.ipynb_checkpoints', 'mymodule1.py', 'addapp.py', 'module1-day4.ipynb']
def listpy(dirpath):
files = os.listdir(dirpath)
return [file for file in files if file.endswith(".py")]
listpy(os.getcwd())
['mymodule.py', 'square.py', 'add.py', 'aad.py', 'mymodule1.py', 'addapp.py']
listpy(".") # . means current directory
['mymodule.py', 'square.py', 'add.py', 'aad.py', 'mymodule1.py', 'addapp.py']
os.listdir()
['index.html', 'module1-day5.html', 'mymodule.py', 'push', 'module1-day5.ipynb', 'square.py', '__pycache__', 'Untitled.html', 'index.ipynb', 'module1-day1.html', 'module1-day1.ipynb', 'module1-day2.ipynb', 'add.py', 'module1-assignment.ipynb', 'aad.py', 'module2-day1.ipynb', 'module1-day3.html', 'module1-day2.html', 'module1-day3.ipynb', 'module1-assignment.html', 'module1-day4.html', 'Makefile', 'module2-day1.html', '.ipynb_checkpoints', 'mymodule1.py', 'addapp.py', 'module1-day4.ipynb']
def listpy(dirpath=None):
if dirpath==None:
dirpath = os.getcwd()
files = os.listdir(dirpath)
return [file for file in files if file.endswith(".py")]
listpy() #means dirpath argument will be taken as NOne
['mymodule.py', 'square.py', 'add.py', 'aad.py', 'mymodule1.py', 'addapp.py']
def listpy_(): # this will work only for current directoty
dirpath = os.getcwd()
files = os.listdir(dirpath)
return [file for file in files if file.endswith(".py")]
listpy("../arcesium_finop_batch1/")
['bank1.py', 'tail.py', 'bank0.py', 'unixcommand.py', 'capitalize.py', 'modulezyx.py', 'mymodule.py', 'hello.py', 'grep.py', 'square.py', 'head.py', 'head1.py', 'mymathmodule.py', 'cat.py', 'just_another.py', 'testmodule.py', 'mymath.py', 'testmodule1.py', 'helloworld.py', 'mymodule1.py']
def factors(n):
return [f for f in range(1, n+1) if n%f==0]
factors(5)
[1, 5]
factors(12)
[1, 2, 3, 4, 6, 12]
factors(49)
[1, 7, 49]
def is_prime(n):
return factors(n)==[1,n]
[p for p in range(50) if is_prime(p)]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
EXAMPLE
COUNTIFS implement this excel function
import operator as op
def seperate_value_cond(condstr):
"<40"
"<>40"
value = int("".join([c for c in condstr if c.isdigit()]))
cond = "".join([c for c in condstr if not c.isdigit()])
return cond, value
def COUNTIFS(condlist, condstr):
funcmap = {">": op.gt,
">=": op.ge,
"<": op.lt,
"<=": op.le,
"<>": op.ne,
"":op.eq}
cond, value = seperate_value_cond(condstr)
return len([v for v in condlist if funcmap[cond](v, value)])
COUNTIFS([10, 20, 30, 40, 50, 40, 40, 50], "<40")
3
COUNTIFS([10, 20, 30, 40, 50, 40, 40, 50], ">=40")
5
def SUMIFS(sumlist, condlist, condstr):
funcmap = {">": op.gt,
">=": op.ge,
"<": op.lt,
"<=": op.le,
"<>": op.ne,
"":op.eq}
cond, value = seperate_value_cond(condstr)
return sum([v for v,c in zip(sumlist, condlist) if funcmap[cond](c, value)])
SUMIFS([1, 2, 3, 4, 5, 4, 4, 5], [10, 20, 30, 40, 50, 40, 40, 50], "<40")
6