Jan 16-20, 2023 Vikrant Patil
All notes are available online at https://notes.pipal.in/2023/arcesium_finop_jan/
© Pipal Academy LLP
%%file file.txt
I can create a file
using jupyter
Writing file.txt
!python hello.py vikrant arg1 arg1
Hello
a = [1, 2, 3, 5, 6] #
a
[1, 2, 3, 5, 6]
a[0] # you will get 1 and [
1
b = '[' + "1" + "," + "2" + "," + "3" + "]"
b # b is not a list it is str
'[1,2,3]'
%%file box.py
import sys
word = sys.argv[1] # make sure that you out appropriate argument from all the argument
topline = "+-" + "-"*len(word) + "-+"
bottomline = topline
print(topline)
print("| " + word + " |")
print(bottomline)
Writing box.py
!python box.py python
+--------+ | python | +--------+
%%file boxthesentence.py
import sys
words = sys.argv[1:] # make sure that you out appropriate argument from all the argument
sentence = " ".join(words)
topline = "+-" + "-"*len(sentence) + "-+"
bottomline = topline
print(topline)
print("| " + sentence + " |")
print(bottomline)
Overwriting boxthesentence.py
!python boxthesentence.py This is more than just a word
+-------------------------------+ | This is more than just a word | +-------------------------------+
def group(a,b):
x = list(range(int((len(a))/b),len(a),b+1))
for num in x:
a.insert(int(num),"/") # generally you don't modify the arguments!
string = str(a) #
y = string.replace("[","").replace("]","") #
return y.split("/")
nums = [1, 2, 3, 4, 5, 6]
str(nums) # this is almost never required!
'[1, 2, 3, 4, 5, 6]'
def group(a,b):
for num in a:
b=[]
f=[]
h=[]
j=[]
tableofthree=str(b)
tableofthree1=str(f)
tableofthree2=str(h)
tableofthree3=str(j)
if len(tableofthree)<=b:
tableofthree.append(num)
elif len(tableofthree1)<=b:
tableofthree1.append(num)
elif len(tableofthree2)<=b:
tableofthree2.append(num)
else len(tableofthree3)<=b:
tableofthree3.append(num)
return join(",",tableofthree,tableofthree1,tableofthree2,tableofthree3)
Cell In[18], line 17 else len(tableofthree3)<=b: ^ SyntaxError: expected ':'
x = "text"
x.append("x")
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[20], line 1 ----> 1 x.append("x") AttributeError: 'str' object has no attribute 'append'
# [1, 2, 3, 4, 5, 6, 7, 8, 9] -> divide into groups of three
#
def group(items, groupsize):
groups = []
for start in range(0, len(items), groupsize):
groups.append(items[start:start+groupsize])
return groups
group([1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
nums
[1, 2, 3, 4, 5, 6]
nums[0:3] # it will take number starting from index 0 till index 3 (excluded)
[1, 2, 3]
list(range(0, 20, 2))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
list(range(0, 20, 3))
[0, 3, 6, 9, 12, 15, 18]
nums[3:6]
[4, 5, 6]
def group(items, groupsize):
groups = []
for start in range(0, len(items), groupsize):
groups.append(items[start:start+groupsize])
return groups
items = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(0, len(items), 4))
[0, 4, 8]
items[0:0+4]
[1, 2, 3, 4]
items[4:4+4]
[5, 6, 7, 8]
items[8:12]
[9]
items[5:100] # this will not fail, it will stop after the end of list has come
[6, 7, 8, 9]
items[100]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[35], line 1 ----> 1 items[100] IndexError: list index out of range
"-".join(["one", "two", "three"])
'one-two-three'
join("-", ["one", "two", "three"]) # this is invalid
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[37], line 1 ----> 1 join("-", ["one", "two", "three"]) # this is invalid NameError: name 'join' is not defined
import os
os.path.join("/", "home", "vikrant") # this is a function inside os.path module
'/home/vikrant'
"".join(["a", "b"]) # is a method inside string ""
'ab'
def group(items, groupsize):
groups = []
for start in range(0, len(items), groupsize):
groups.append(items[start:start+groupsize])
print(groups) # this will fail in unittests!
def group(items, groupsize):
groups = []
for start in range(0, len(items), groupsize):
groups.append(items[start:start+groupsize])
return groups # this will fail in unittests!
import os
os.path.sep
'/'
Daily average prices of some symbols are given each for five days of a week. Data consists of list of records for each day. Every record consists of (symbol, day, stock price). Write a function weekly_average to compute weekly average for given symbol using this data. it should work as given below
>>> prices = [('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)]
>>> weekly_average(prices, "APPLE")
324.51215324332736
prices = [('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)]
type(prices)
list
for item in prices:
print(item)
('IBM', 'Monday', 111.71436961893693)
('IBM', 'Tuesday', 141.21220022208635)
('IBM', 'Wednesday', 112.40571010053796)
('IBM', 'Thursday', 137.54133351926248)
('IBM', 'Friday', 140.25154281801224)
('MICROSOFT', 'Monday', 235.0403622499107)
('MICROSOFT', 'Tuesday', 225.0206535036475)
('MICROSOFT', 'Wednesday', 216.10342426936444)
('MICROSOFT', 'Thursday', 200.38038844494193)
('MICROSOFT', 'Friday', 235.80850482793264)
('APPLE', 'Monday', 321.49182055844256)
('APPLE', 'Tuesday', 340.63612771662815)
('APPLE', 'Wednesday', 303.9065277507285)
('APPLE', 'Thursday', 338.1350605764038)
('APPLE', 'Friday', 318.3912296144338)
for item in prices:
print(item[0])
IBM IBM IBM IBM IBM MICROSOFT MICROSOFT MICROSOFT MICROSOFT MICROSOFT APPLE APPLE APPLE APPLE APPLE
for item in prices:
print(item[1])
Monday Tuesday Wednesday Thursday Friday Monday Tuesday Wednesday Thursday Friday Monday Tuesday Wednesday Thursday Friday
for item in prices:
print(item[2])
111.71436961893693 141.21220022208635 112.40571010053796 137.54133351926248 140.25154281801224 235.0403622499107 225.0206535036475 216.10342426936444 200.38038844494193 235.80850482793264 321.49182055844256 340.63612771662815 303.9065277507285 338.1350605764038 318.3912296144338
for ticker,day,value in prices:
print(ticker, day, value)
IBM Monday 111.71436961893693 IBM Tuesday 141.21220022208635 IBM Wednesday 112.40571010053796 IBM Thursday 137.54133351926248 IBM Friday 140.25154281801224 MICROSOFT Monday 235.0403622499107 MICROSOFT Tuesday 225.0206535036475 MICROSOFT Wednesday 216.10342426936444 MICROSOFT Thursday 200.38038844494193 MICROSOFT Friday 235.80850482793264 APPLE Monday 321.49182055844256 APPLE Tuesday 340.63612771662815 APPLE Wednesday 303.9065277507285 APPLE Thursday 338.1350605764038 APPLE Friday 318.3912296144338
def findfiles(folderpath, prefix, extension):
import os
for file in os.listdir(folderpath):
new_list=[]
if file.startswith(prefix) and file.endswith(extension):
new_list.append(file)
return new_list
findfiles(".", "", ".py")
['box.py']
!ls
args.py Makefile module1-day4.org other_files basicstats.py Makefile~ module1-day5.html push box.py module1-day1.html module1-day5.ipynb testdir boxthesentence.py module1-day1.ipynb module1-day5.org testdir1 file.txt module1-day2.html module1-day5.org~ test.html greeting.py module1-day2.ipynb module1-nurturing.html test.ipynb hello1.py module1-day3.html module1-nurturing.ipynb test.txt hello.py module1-day3.ipynb mysum.py Untitled.html index.html module1-day4.html nurturing_session1.org users.csv~ index.ipynb module1-day4.ipynb nurturing_session1.org~
def findfiles(folderpath, prefix, extension):
import os
new_list=[]
for file in os.listdir(folderpath):
if file.startswith(prefix) and file.endswith(extension):
new_list.append(file)
return new_list
findfiles(".", "", ".py")
['hello1.py', 'hello.py', 'boxthesentence.py', 'basicstats.py', 'greeting.py', 'mysum.py', 'args.py', 'box.py']
prices
[('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)]
def get_value(record):
return record[2]
max(prices, key=get_value)
('APPLE', 'Tuesday', 340.63612771662815)
sorted(prices, key=get_value)
[('IBM', 'Monday', 111.71436961893693),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('IBM', 'Tuesday', 141.21220022208635),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Friday', 318.3912296144338),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Tuesday', 340.63612771662815)]
words = "one two three four five six seven eight nine ten".split()
words
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
max(words, key=len)
'three'
sorted(words)
['eight', 'five', 'four', 'nine', 'one', 'seven', 'six', 'ten', 'three', 'two']
sorted(words, key=len)
['one', 'two', 'six', 'ten', 'four', 'five', 'nine', 'three', 'seven', 'eight']
add
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[70], line 1 ----> 1 add NameError: name 'add' is not defined
def add(a, b):
return a + b
add # this is a variable which happen to contain function
<function __main__.add(a, b)>
add
<function __main__.add(a, b)>
print(add)
<function add at 0x7f8f467dc280>
def sumof(x, y, func):
return func(x) + func(y)
def square(x):
return x*x
sumof(4, 5, square)
41
max(words, key=len) # to find max one has to put a loop... and here built in max is doing that for us
'three'
sorted(prices, key=get_value) # just passing function as a variable ... not calling
[('IBM', 'Monday', 111.71436961893693),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('IBM', 'Tuesday', 141.21220022208635),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Friday', 318.3912296144338),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Tuesday', 340.63612771662815)]
get_value(('IBM', 'Monday', 111.71436961893693))
111.71436961893693
records = [
("TATA", 200.0, 5.5),
("INFY", 2000.0, -5),
("RELIANCE", 1505.5, 50.0),
("HCL", 1200, 70.5)
]
def get_value(r):
return r[1]
def get_gain(r):
return r[2]
sorted(records, key=get_value)
[('TATA', 200.0, 5.5),
('HCL', 1200, 70.5),
('RELIANCE', 1505.5, 50.0),
('INFY', 2000.0, -5)]
sorted(records, key=get_gain)
[('INFY', 2000.0, -5),
('TATA', 200.0, 5.5),
('RELIANCE', 1505.5, 50.0),
('HCL', 1200, 70.5)]
A clean room has to maintain constant temperature with some margin allowed depending on experiment.
A list has deviation of temperature of the room during one experiment. We have to find the maximum deviation.
Write a function key which will be used as key argument to max function to find maximum temperature fluctuation from a list of values.
>>> fluctuations = [1,1,-1,2,-5,3,4,1,2,1]
>>> max(fluctuations, key=key)
-5
fluctuations = [1,1,-1,2,-5,3,4,1,2,1]
max(fluctuations)
4
sorted(fluctuations)
[-5, -1, 1, 1, 1, 1, 2, 2, 3, 4]
abs(-5)
5
max(fluctuations, key=abs) # key argument is not always index
-5
turnover = ["1B", "1.2B", "1000K", "3M"]
def convert(entry):
if entry.endswith("B"):
return float(entry[:-1])*1e9
elif entry.endswith("M"):
return float(entry[:-1])*1e6
elif entry.endswith("K"):
return float(entry[:-1])*1000
else:
raise Exception("Invalid suffix")
max(turnover) # it is text, so we will get by alphebetical order
'3M'
max(turnover, key=convert)
'1.2B'
dom
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[102], line 1 ----> 1 dom NameError: name 'dom' is not defined
convert("34Z")
--------------------------------------------------------------------------- Exception Traceback (most recent call last) Cell In[103], line 1 ----> 1 convert("34Z") Cell In[99], line 9, in convert(entry) 7 return float(entry[:-1])*1000 8 else: ----> 9 raise Exception("Invalid suffix") Exception: Invalid suffix