Python Virtual Training For Arcesium - Module I - Day 5¶

Jan 16-20, 2023 Vikrant Patil

All notes are available online at https://notes.pipal.in/2023/arcesium_finop_jan/

Please login to https://engage.pipal.in/ and launch jupyter lab

For today create a notebook with name module1-day5

notebook names are case sensitive. Make sure you give correct name

© Pipal Academy LLP

Some Hints for problems¶

  • Write a function group that take a list of values and splits into smaller lists of given size.
>>> group([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

>>> group([1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
  1. Can you make use of slicing
  2. make use of range
In [1]:
nums = [4, 5, 73, 2, 5, 8]
In [3]:
for num in nums:
    print(num, end=",")
4,5,73,2,5,8,
In [4]:
nums[:3] # first three
Out[4]:
[4, 5, 73]
In [5]:
nums[3:6]
Out[5]:
[2, 5, 8]
In [15]:
for i in range(len(nums)):
    print(nums[i]) # i is index here
4
5
73
2
5
8
In [8]:
evens = list(range(2, 20, 2))
In [9]:
evens
Out[9]:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
In [10]:
evens[0::3]
Out[10]:
[2, 8, 14]
In [11]:
evens[1::3]
Out[11]:
[4, 10, 16]
In [12]:
nums[3:100] # if end is given beyond boundary .. then slicing does not fail.. 
Out[12]:
[2, 5, 8]
In [13]:
nums[100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[13], line 1
----> 1 nums[100]

IndexError: list index out of range
In [14]:
[1, 2, 3, 4, 5, 6, 7]
Out[14]:
[1, 2, 3, 4, 5, 6, 7]
In [ ]:
[1, 2, 3], [4, 5, 6] , [7]

While loop¶

while condition:
    code line 1
    code line 2
    chaning of condition
In [16]:
x = 5

while x!=0:
    print(x)
    # unless you change x ..in the loop so that it becomes 0 some time, this while loop will keep on exuecuting
    x = x - 1
5
4
3
2
1
In [17]:
def myrange(n):
    nums = []
    
    while n>=0: # condition if not given correctly you will end with bugs in code!
        n = n - 1
        nums.append(n) 
        
    return nums
In [18]:
myrange(5)
Out[18]:
[4, 3, 2, 1, 0, -1]
In [21]:
def myrange(n):
    nums = []
    
    n = n - 1 
    while n>=0: # condition if not given correctly you will end with bugs in code!
        nums.append(n) 
        n = n - 1
        
    nums.reverse()
    return nums
In [22]:
myrange(5)
Out[22]:
[0, 1, 2, 3, 4]
In [27]:
def myrange(n):
    nums = []
    
    while n>0: # condition if not given correctly you will end with bugs in code!
        n = n - 1
        nums.append(n) 
        
    nums.reverse()
    return nums
In [28]:
myrange(5)
Out[28]:
[0, 1, 2, 3, 4]
In [29]:
while True:
    pass
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[29], line 1
----> 1 while True:
      2     pass

KeyboardInterrupt: 

Example: Fibonnacci sequence¶

$F_n$ = $F_n$$-$$_1$ + $F_n$$-$$_2$

current = prev_current + previous

prev_current = current

In [31]:
def print_fib(n):
    """
    Print fibonnacci numbers less than n
    """
    
    curr, prev = 1, 1
    
    while prev < n:
        print(prev, end=",")  
        curr, prev = prev+curr, curr
    
In [30]:
a, b, = 2, 3
In [32]:
print_fib(100)
1,1,2,3,5,8,13,21,34,55,89,

Modules¶

  • third party python packege - these are python libraries which are not packaged in python by default
  • modules (built in libraries ..but not accessible by default, you will have to import them before using
In [33]:
int("45") # these are available directly.. no need of import.. these are builtin functions
Out[33]:
45
In [34]:
import math # this will import math module which has mathematics related functions
In [35]:
math.pi
Out[35]:
3.141592653589793
In [36]:
math.sin(math.pi)
Out[36]:
1.2246467991473532e-16
In [37]:
math.sqrt(4)
Out[37]:
2.0
In [38]:
math.ceil(2.3)
Out[38]:
3
In [39]:
math.floor(2.3)
Out[39]:
2
In [49]:
import math
In [41]:
import math as m # you can give some nickname to your import
In [42]:
m.pi
Out[42]:
3.141592653589793
In [44]:
from math import sin # only function is function is imported
In [45]:
sin(0)
Out[45]:
0.0
In [50]:
del math # del actually is not anything to math module... but delinking the name math and math-module
In [47]:
math
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[47], line 1
----> 1 math

NameError: name 'math' is not defined

Some important builtin modules¶

In [51]:
import os

Where is this interpreter running

In [52]:
os.getcwd() # give me current working directory
Out[52]:
'/home/vikrant/trainings/2023/arcesium_finop_jan'
In [53]:
current_folder = os.getcwd()

os.listdir(current_folder)
Out[53]:
['index.html',
 'test.ipynb',
 'module1-day5.html',
 'module1-day4.org',
 'push',
 'module1-day5.ipynb',
 'Untitled.html',
 'index.ipynb',
 'module1-day1.html',
 'other_files',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'test.html',
 'module1-day3.html',
 'module1-day2.html',
 'module1-day3.ipynb',
 'Makefile~',
 'module1-day4.html',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day5.org',
 'users.csv~',
 'module1-day4.ipynb']

Some commonly used functionality from os module

  • getting current working directory - os.getcwd()
  • finding files from given directory - os.listdir()
  • check if given path is file or directory!
In [55]:
os.listdir() # if not input /argument is given then by default current working directory listing is returned
Out[55]:
['index.html',
 'test.ipynb',
 'module1-day5.html',
 'module1-day4.org',
 'push',
 'module1-day5.ipynb',
 'Untitled.html',
 'index.ipynb',
 'module1-day1.html',
 'other_files',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'test.html',
 'module1-day3.html',
 'module1-day2.html',
 'module1-day3.ipynb',
 'Makefile~',
 'module1-day4.html',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day5.org',
 'users.csv~',
 'module1-day4.ipynb']
In [56]:
help(os.getlogin)
Help on built-in function getlogin in module posix:

getlogin()
    Return the actual login name.

In [57]:
os.path.isfile("index.html")
Out[57]:
True
In [58]:
os.path.isfile(os.getcwd())
Out[58]:
False
In [59]:
os.path.isdir(os.getcwd())
Out[59]:
True
In [60]:
os.path.isfile(os.getcwd())
Out[60]:
False
In [61]:
os.path
Out[61]:
<module 'posixpath' from '/usr/lib/python3.10/posixpath.py'>
In [62]:
os.path.isfile("/home/vikrant/")
Out[62]:
False
In [63]:
path = "c:\\program files\\python\\bin\\python.exe"
In [73]:
folder = "/home/vikrant/trainings/2023/arcesium_finop_jan"
In [74]:
os.path.sep
Out[74]:
'/'
In [75]:
filepath =  folder + os.path.sep + "index.html"
In [72]:
filepath
Out[72]:
'/home/vikrant/trainings/2023/arcesium_finop_jan/index.html'
In [77]:
",".join(["one", "two"])
Out[77]:
'one,two'
In [78]:
os.path.join(folder, "index.html")
Out[78]:
'/home/vikrant/trainings/2023/arcesium_finop_jan/index.html'
In [79]:
os.path.join(folder, "other_files", "users.csv")
Out[79]:
'/home/vikrant/trainings/2023/arcesium_finop_jan/other_files/users.csv'
In [80]:
os.path.join(os.getcwd(), "other_files", "users.csv")
Out[80]:
'/home/vikrant/trainings/2023/arcesium_finop_jan/other_files/users.csv'
In [82]:
os.getlogin()
Out[82]:
'vikrant'
In [84]:
os.mkdir("testdir") # relative path
In [88]:
complete_path = os.path.join(os.getcwd(), "testdir1") # just a text 
In [87]:
complete_path # absoulute path... starts from c: (drive) on windows and / on unix/mac
Out[87]:
'/home/vikrant/trainings/2023/arcesium_finop_jan/testdir1'
In [89]:
os.mkdir(complete_path)

datetime¶

In [90]:
import datetime 
In [91]:
datetime.datetime(2023, 1, 20)
Out[91]:
datetime.datetime(2023, 1, 20, 0, 0)
In [92]:
date = datetime.datetime(2023, 1, 20)
In [94]:
type(date) # this returns class of that object
Out[94]:
datetime.datetime
In [95]:
x = 10
In [96]:
x + 45
Out[96]:
55
In [97]:
date 
Out[97]:
datetime.datetime(2023, 1, 20, 0, 0)
In [98]:
date
Out[98]:
datetime.datetime(2023, 1, 20, 0, 0)
In [99]:
help(datetime.timedelta)
Help on class timedelta in module datetime:

class timedelta(builtins.object)
 |  Difference between two datetime values.
 |  
 |  timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
 |  
 |  All arguments are optional and default to 0.
 |  Arguments may be integers or floats, and may be positive or negative.
 |  
 |  Methods defined here:
 |  
 |  __abs__(self, /)
 |      abs(self)
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __bool__(self, /)
 |      True if self else False
 |  
 |  __divmod__(self, value, /)
 |      Return divmod(self, value).
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __floordiv__(self, value, /)
 |      Return self//value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mod__(self, value, /)
 |      Return self%value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __neg__(self, /)
 |      -self
 |  
 |  __pos__(self, /)
 |      +self
 |  
 |  __radd__(self, value, /)
 |      Return value+self.
 |  
 |  __rdivmod__(self, value, /)
 |      Return divmod(value, self).
 |  
 |  __reduce__(...)
 |      __reduce__() -> (cls, state)
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __rfloordiv__(self, value, /)
 |      Return value//self.
 |  
 |  __rmod__(self, value, /)
 |      Return value%self.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __rsub__(self, value, /)
 |      Return value-self.
 |  
 |  __rtruediv__(self, value, /)
 |      Return value/self.
 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  __sub__(self, value, /)
 |      Return self-value.
 |  
 |  __truediv__(self, value, /)
 |      Return self/value.
 |  
 |  total_seconds(...)
 |      Total seconds in the duration.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  days
 |      Number of days.
 |  
 |  microseconds
 |      Number of microseconds (>= 0 and less than 1 second).
 |  
 |  seconds
 |      Number of seconds (>= 0 and less than 1 day).
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  max = datetime.timedelta(days=999999999, seconds=86399, microseconds=9...
 |  
 |  min = datetime.timedelta(days=-999999999)
 |  
 |  resolution = datetime.timedelta(microseconds=1)

In [100]:
oneday = datetime.timedelta(days=1)
In [101]:
date 
Out[101]:
datetime.datetime(2023, 1, 20, 0, 0)
In [103]:
date + oneday # next date
Out[103]:
datetime.datetime(2023, 1, 21, 0, 0)
In [104]:
date.day
Out[104]:
20
In [105]:
date.year
Out[105]:
2023
In [109]:
date.second
Out[109]:
0
In [110]:
date.hour
Out[110]:
0
In [112]:
now = datetime.datetime.now() # get datetime instance for current time ...
In [113]:
now
Out[113]:
datetime.datetime(2023, 1, 20, 11, 24, 44, 907082)
In [114]:
datetime.datetime.today()
Out[114]:
datetime.datetime(2023, 1, 20, 11, 26, 11, 455696)
In [115]:
list(range(5))
Out[115]:
[0, 1, 2, 3, 4]
In [124]:
def daterange(n):
    today = datetime.datetime.today()
    
    dates = []
    for i in range(n):
        dates.append(today + datetime.timedelta(i))
        
    return dates
In [125]:
daterange(4)
Out[125]:
[datetime.datetime(2023, 1, 20, 11, 32, 18, 210060),
 datetime.datetime(2023, 1, 21, 11, 32, 18, 210060),
 datetime.datetime(2023, 1, 22, 11, 32, 18, 210060),
 datetime.datetime(2023, 1, 23, 11, 32, 18, 210060)]
In [116]:
def add(x=1, y=0):
    return x + y
In [117]:
add(2) # by order of arguments x = 2, y not given
Out[117]:
2
In [118]:
add(2, 3) # x =1 , y =3 # positional arguments
Out[118]:
5
In [119]:
add()
Out[119]:
1
In [121]:
add(x=2, y=4) # named/keyword arguments
Out[121]:
6
In [123]:
add(x=2, 5)
  Cell In[123], line 1
    add(x=2, 5)
              ^
SyntaxError: positional argument follows keyword argument

Writing python scripts¶

In [126]:
%%file test.txt
in this I have
some text data
Writing test.txt
In [ ]:
%%file hello.py
print("Hello")
In [128]:
%%file test1.txt
UsageError: %%file is a cell magic, but the cell body is empty.
In [129]:
%%file hello.py

print("Hello")
Writing hello.py
In [130]:
!ls # with this I can run system commandd
hello.py	    module1-day2.html	module1-day5.html   test.html
index.html	    module1-day2.ipynb	module1-day5.ipynb  test.ipynb
index.ipynb	    module1-day3.html	module1-day5.org    test.txt
Makefile	    module1-day3.ipynb	other_files	    Untitled.html
Makefile~	    module1-day4.html	push		    users.csv~
module1-day1.html   module1-day4.ipynb	testdir
module1-day1.ipynb  module1-day4.org	testdir1
In [131]:
!python3 hello.py
Hello
In [132]:
%%file hello1.py

print("Hello Arcesium Finop Jan batch!")
Writing hello1.py
In [133]:
!python3 hello1.py
Hello Arcesium Finop Jan batch!
In [134]:
!python hello1.py
Hello Arcesium Finop Jan batch!

Python program with command line agruments¶

In [135]:
%%file args.py
import sys

sys.argv # this is a list which comes with the user arguments

print(sys.argv)
Writing args.py
In [136]:
!python args.py
['args.py']
In [137]:
!python args.py test1 test2 skjdhds kdsjfhkjds kjdshf
['args.py', 'test1', 'test2', 'skjdhds', 'kdsjfhkjds', 'kjdshf']
In [138]:
!python args.py arg1
['args.py', 'arg1']
In [139]:
!python args.py arg1 arg2
['args.py', 'arg1', 'arg2']
In [141]:
!python args.py arg1 arg2 arg3 # you can only pass space seperated arguments
['args.py', 'arg1', 'arg2', 'arg3']
In [143]:
!python args.py 1 2 3 4 5 # arguments are always text!
['args.py', '1', '2', '3', '4', '5']
In [144]:
%%file mysum.py
import sys

numargs = sys.argv[1:] # remember sys.argv is a list of text data
                    # 0th item is always python file name ...
s = 0
for n in numargs:
    s = s + n
    
print(s)
Writing mysum.py
In [145]:
!python mysum.py 1 2 3 4 5
Traceback (most recent call last):
  File "/home/vikrant/trainings/2023/arcesium_finop_jan/mysum.py", line 7, in <module>
    s = s + n
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [146]:
%%file mysum.py
import sys

numargs = sys.argv[1:] # remember sys.argv is a list of text data
                    # 0th item is always python file name ...
s = 0
for n in numargs:
    s = s + int(n)
    
print(s)
Overwriting mysum.py
In [147]:
!python mysum.py 1 2 3 4 5 6
21
In [152]:
%%file mysum.py
import sys

def mysum(nums):
    s = 0
    for n in nums:
        s = s + int(n)
    return s
    

numargs = sys.argv[1:] # remember sys.argv is a list of text data
                      # 0th item is always python file name ...
print(mysum(numargs))
Overwriting mysum.py
In [151]:
!python mysum.py 1 2 3 4
10
In [155]:
%%file greeting.py
import sys

print("hello", sys.argv[1])
Overwriting greeting.py
In [156]:
!python greeting.py vikrant
hello vikrant
In [157]:
!python greeting.py python
hello python
In [161]:
%%file basicstats.py
import sys

def mysum(nums):
    s = 0
    for n in nums:
        s = s + n
    return s
    
    
def std(nums):
    pass

def mean(nums):
    pass

def quantile(nums, percentage):
    pass
    
def to_ints(numargs):
    nums = []
    
    for sn in numargs:
        nums.append(int(sn))
        
    return nums

numargs = sys.argv[1:] # remember sys.argv is a list of text data
                      # 0th item is always python file name ...
    
nums = to_ints(numargs)
print(mysum(nums))
print(std(nums))
print(mean(nums))
Overwriting basicstats.py
In [162]:
!python basicstats.py 1 2 3 4 5
15
None
None
In [ ]: