Jul 7, 2022 Vikrant Patil
All notes are available online at https://notes.pipal.in/2022/arcesium_finop_batch1/
Please accept the invitation that you have received in your email and login to
© Pipal Academy LLP
5 * 4
20
3 + 4
5**2
[1]*3 # jupyter's behavior
[1, 1, 1]
There are two types of problems that we have in assignment
def add(x, y):
print(x+y) # test will fail
add(4, 5)
9
def add(x, y):
return x+y
%%file add.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
def add_ints(x, y):
return x+y
add_ints(a, b) # test wil fail bebasue we are not printing the result
# if you don't give this print, test will fail
Overwriting add.py
!python add.py 2 3
%%file add.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
def add_ints(x, y):
return x+y
print(add_ints(a, b)) # if you don't give this print, test will fail
Overwriting add.py
!python add.py 4 5
9
nums = [1, 2, 3, 4, 5]
text = "this is some text"
test_tuple = (1, 2, 3, 4, 5, 5, 6)
for n in nums:
print(n*n)
1 4 9 16 25
cubes = []
for n in test_tuple:
cubes.append(n**3)
cubes
[1, 8, 27, 64, 125, 125, 216]
def mean(nums):
"""mean([1, 2, 3, 4, 5])
3.0
"""
return (nums[0] + nums[1] + nums[2] + nums[3] + nums[4])/5
mean([34, 45, 56])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [19], in <cell line: 1>() ----> 1 mean([34, 45, 56]) Input In [18], in mean(nums) 1 def mean(nums): 2 """mean([1, 2, 3, 4, 5]) 3 3.0 4 """ ----> 6 return (nums[0] + nums[1] + nums[2] + nums[3] + nums[4])/5 IndexError: list index out of range
def mean(nums):
"""mean([1, 2, 3, 4, 5])
3.0
"""
s = 0
for n in nums:
s += n # this is same as s = s + n
return s/len(nums)
mean([23, 34, 45])
34.0
mean([])# won't work
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Input In [23], in <cell line: 1>() ----> 1 mean([]) Input In [20], in mean(nums) 6 for n in nums: 7 s += n # this is same as s = s + n ----> 8 return s/len(nums) ZeroDivisionError: division by zero
len([])
0
def mean(nums):
"""mean([1, 2, 3, 4, 5])
3.0
"""
if len(nums) == 0:
return 0
s = 0
for n in nums:
s += n # this is same as s = s + n
return s/len(nums)
%%file add1.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
def add_ints(x, y):
return x+y # here is it allowed!
return add_ints(a, b) # for scripts only print statement is the mechanism to tell user what is output!
# return statements are only part of a function
Writing add1.py
!python add1.py 2 3
File "/home/vikrant/trainings/2022/arcesium_finop_batch1/add1.py", line 10
return add_ints(a, b) # for scripts only print statement is the mechanism to tell user what is output!
^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'return' outside function
Q: How is while loop different from for loop and whats the use case
nums = [34, 454 , 343, 3, 5]
find sum of first 1000 numbers
sum1000 = 0
count = 1
while count <= 1000:
sum1000 += count
count += 1
print(sum1000)
500500
s = 0
for i in range(1, 1001):
s += i
print(s)
500500
stocks = {"IBM": 123,
"APPLE": 150,
"AGILENT": 300,
"AT&T": 200}
stocks['IBM'] # access by key
123
for item in stocks: # this will loop over keys
print(item)
IBM APPLE AGILENT AT&T
for key, value in stocks.items():# loop over key and value
print(key, value)
IBM 123 APPLE 150 AGILENT 300 AT&T 200
for value in stocks.values():# loop over values
print( value)
123 150 300 200
x, y = 3, 4
x, y = [5, 6]
a, b = (3, 4)
a
3
b
4
# name, value and volume
records = [("name1", 34, 678),
("name2", 45, 343),
("name3", 100, 50)]
for record in records:
print(record)
('name1', 34, 678)
('name2', 45, 343)
('name3', 100, 50)
for name, value, volume in records: # you have to know before hand that it has 3 items
if value > 50:
print("This has value greater than 50")
print(name, volume)
This has value greater than 50 name3 50
import os
def longlistdir(folder):
files_dirs = os.listdir(folder)
for filename in files_dirs:
if os.path.isfile(filename):
print("f", filename)
else:
print("d", filename)
longlistdir("..")
d data.txt d arcesium_finop_batch1
os.path.isfile("sadjkjshfkjds hf")
False
def longlistdir(folder):
files_dirs = os.listdir(folder)
for filename in files_dirs:
filepath = os.path.join(folder, filename)
if os.path.isfile(filepath):
print("f", filename)
else:
print("d", filename)
longlistdir("..")
f data.txt d arcesium_finop_batch1
longlistdir(".")
f index.html f mystas.py f nurturing-session.ipynb f module1-day5.html f arguments.py f day2.txt f push f hello.py f sqaure.py f module1-day5.ipynb f nurturing1.org d __pycache__ f index.ipynb f module1-day1.html f module1-day1.ipynb f module1-day2.ipynb f add.py f module1-day3.html f module1-day2.html f day3.txt f day1.txt f nurturing-session.html f module1-day3.ipynb f module1-day4.html f Makefile d .ipynb_checkpoints f nurturing1.org~ f add1.py f module1-day4.ipynb
def longlistdir_deep(folder):
files_dirs = os.listdir(folder)
for filename in files_dirs:
filepath = os.path.join(folder, filename)
if os.path.isfile(filepath):
print("f", filename)
else:
print("d", filename)
longlistdir_deep(filepath)
longlistdir_deep("..")
f data.txt d arcesium_finop_batch1 f index.html f mystas.py f nurturing-session.ipynb f module1-day5.html f arguments.py f day2.txt f push f hello.py f sqaure.py f module1-day5.ipynb f nurturing1.org d __pycache__ f mystas.cpython-310.pyc f index.ipynb f module1-day1.html f module1-day1.ipynb f module1-day2.ipynb f add.py f module1-day3.html f module1-day2.html f day3.txt f day1.txt f nurturing-session.html f module1-day3.ipynb f module1-day4.html f Makefile d .ipynb_checkpoints f nurturing-session-checkpoint.ipynb f module1-day4-checkpoint.ipynb f index-checkpoint.ipynb f module1-day2-checkpoint.ipynb f index-checkpoint.html f module1-day3-checkpoint.ipynb f module1-day5-checkpoint.ipynb f module1-day1-checkpoint.ipynb f nurturing1.org~ f add1.py f module1-day4.ipynb
def longlistdir_deep(folder, prefix=""):
files_dirs = os.listdir(folder)
for filename in files_dirs:
filepath = os.path.join(folder, filename)
if os.path.isfile(filepath):
print(prefix, "f", filename)
else:
print(prefix, "d", filename)
longlistdir_deep(filepath, prefix + " ")
longlistdir_deep("..")
f data.txt
d arcesium_finop_batch1
f index.html
f mystas.py
f nurturing-session.ipynb
f module1-day5.html
f arguments.py
f day2.txt
f push
f hello.py
f sqaure.py
f module1-day5.ipynb
f nurturing1.org
d __pycache__
f mystas.cpython-310.pyc
f index.ipynb
f module1-day1.html
f module1-day1.ipynb
f module1-day2.ipynb
f add.py
f module1-day3.html
f module1-day2.html
f day3.txt
f day1.txt
f nurturing-session.html
f module1-day3.ipynb
f module1-day4.html
f Makefile
d .ipynb_checkpoints
f nurturing-session-checkpoint.ipynb
f module1-day4-checkpoint.ipynb
f index-checkpoint.ipynb
f module1-day2-checkpoint.ipynb
f index-checkpoint.html
f module1-day3-checkpoint.ipynb
f module1-day5-checkpoint.ipynb
f module1-day1-checkpoint.ipynb
f nurturing1.org~
f add1.py
f module1-day4.ipynb
def fib(n):
"""nth fibonacci is sum of (n-1)th fibonacci and (n-2)th fibonacci
"""
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
``` just to show how this will work def dirsize(folder): files = #listfiles size = 0 for f in files: if f is file: size += sizeof(f) else: size += dirsize(folder/f)
return size
```
def hello():
print("hello")
Implement excel function SUMIFS as a function in python.
SUMIFS(sum_list, criteria_list, condition). Here first argument is the list on
which sum will be performed. Second argument is the list on which condition is
checked, and third argument is condition as a string , as in excel.
possible values of condition are
Sample run is shown below. For simplicity assume that all the data consists of integers.
>>> d = [1,2,3,4,5,4,4,5]
>>> a = [10,20,30,40,50,40,40,50]
>>> SUMIFS(d, a, "<40")
6
>>> SUMIFS(d, a, ">=40")
22
>>> SUMIFS(d, a, "40")
12
>>> SUMIFS(d, a, "<>40")
16
def lessthaneq(sumlist, criterio_list, condition):
num = int(condition[2:])
s = 0
for i in range(len(sumlist)):
if criterio_list[i] <= num:
s += sumlist[i]
return s
def SUMIFS(sum_list, criterio_list, condition):
s = 0
if "<>" in condition:
pass
elif "<=" in condition:
return lessthaneq(sum_list, criterio_list, condition)
elif "<" in condition:
pass
elif ">=" in condition:
pass
elif ">" in condition:
pass
else:
# case for equal
pass
d = [1,2,3,4,5,4,4,5]
a = [10,20,30,40,50,40,40,50]
SUMIFS(d, a, "<=50")
28