Module 1 - Day 5

Login to Lab using your credentials. There is a notebook with name 1-5.ipynb already created for you. Open that and use it for today’s training.

Shut down all previous notebooks.

Quick recap

  • Basic data types
  • Collections
  • built in functions - max, min, sum, sorted, len, range, list
  • methods from string - startswith, endswith , split, join
  • methods lists - append, insert, remove, reverse
  • custom functions
  • conditions and loops

built in modules

import random # once this import statement is executed ....there will be a variable with name random
random
<module 'random' from '/opt/tljh/user/lib/python3.10/random.py'>
len
<function len(obj, /)>
def foo():
    pass # empty statement
foo
<function __main__.foo()>
ones = [1, 1, 1, 1]
ones.index(1)
0
random.random() # with generate a random number between 0 and 1
0.17037947163424505

problem

Write a function generate_randoms which will gnerate a list of n random numbers (value between 0-1)

import random

def generate_randoms(n): # n is integer


    
for i in range(5):
    print(i)
0
1
2
3
4
nums = []
nums.append(0)
random.random()
0.9902054030841536
range(5)
range(0, 5)
for i in range(5):
    print(i)
0
1
2
3
4
for i in range(5):
    print(i, end=",") # end the print with "," instead of \n
0,1,2,3,4,
for i in range(5):
    print(i, end=" ")
0 1 2 3 4 
import random

def generate_randoms(n): # n is integer
    randnums = []

    for i in range(n):
        r = random.random()
        randnums.append(r)

    return randnums

    
generate_randoms(3)
[0.009110195281217481, 0.5968671809207017, 0.7455438096633993]
generate_randoms(8)
[0.5552308671158126,
 0.3699835115213501,
 0.06719071835236257,
 0.8500590744050013,
 0.3948638112419627,
 0.49646221176988414,
 0.7966689902599045,
 0.9940776054647534]
import math
import os
import math as m # this is like giving alias to the module name
m.sin(0)
0.0
import pandas as pd # this is thrid party module
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[29], line 1
----> 1 import pandas as pd # this is thrid party module

ModuleNotFoundError: No module named 'pandas'
import numpy as np
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[30], line 1
----> 1 import numpy as np

ModuleNotFoundError: No module named 'numpy'

os module

import os
os.getcwd() # working directory of python
'/opt/arcesium-python-2024-june'
os.path # is submodule inside os module
<module 'posixpath' from '/opt/tljh/user/lib/python3.10/posixpath.py'>
# path  absolute path, relative path
# absoulte path c:\\Program Files\\Python\bin\python.exe
# relative path - relative to current working directory
os.path.isdir(path) # this will check if given path is folder or not
#os.listdir - > list files from given directory

os.listdir(os.getcwd()) # it returns names of all files and folders from 
['.ipynb_checkpoints',
 'lab.qmd',
 '1-3.ipynb',
 'index.qmd',
 'about.qmd',
 '_site',
 '.gitignore',
 'styles.css',
 '1-1.ipynb',
 '.quarto',
 'syllabus.qmd',
 '1-2.ipynb',
 'schedule.qmd',
 '1-5.ipynb',
 '_quarto.yml',
 '.git',
 'README.md',
 '1-4.ipynb']
os.listdir("/home/jupyter-pipal/")
['.config',
 '.ipynb_checkpoints',
 'training',
 'final',
 '.python_history',
 '.bash_logout',
 'users.txt',
 'hello-world.ipynb',
 'startup.py',
 '.ipython',
 '.jupyterhub-token.txt',
 '.ssh',
 '.bashrc',
 '.gitconfig',
 'sync.py',
 'setup.ipynb',
 '.cache',
 '.wget-hsts',
 '.viminfo',
 '.jupyter',
 '.lesshst',
 'setup_magic.sh',
 '.profile',
 '.bash_history',
 '.local']
os.listdir("test") # this is relative path with respect current working directory
['.ipynb_checkpoints', 'test.txt']
os.getcwd()
'/opt/arcesium-python-2024-june'
os.listdir("/opt/arcesium-python-2024-june/test") # this is absolute path
['.ipynb_checkpoints', 'test.txt']

whenever you construct path using python program make sure to use os.path module

folders = ["Program Files", "Python"]
os.path.join("Program Files", "Python") # on windows it take \\ and on linux/unix/mac it will take /
'Program Files/Python'
import time
time.sleep(2) # sleep for two seconds
def tick(n):
    for i in range(n):
        time.sleep(1)
        print("tick-", i)

    print("BOOOM....")
tick(5)
tick- 0
tick- 1
tick- 2
tick- 3
tick- 4
BOOOM....
[1, 1, 2, 4, 5, 6]
["_site", "test", "_quarto.yml", "1-1.ipynb"....]

problem

Write a function find_notebooks from current working directory notebook is a file with extension .ipynb

hints: you will have go over all the files from current working directory os.listdir()

find_notebooks()
["1-1.ipynb", "1-2.ipynb", "1-3.ipynb", "1-4.ipynb"]
import os

def find_notebooks():
    files = os.listdir()  
    notebooks = []
    for file in files:
        if file.endswith(".ipynb"):
            notebooks.append(file)
    return notebooks
find_notebooks()
['1-3.ipynb', '1-1.ipynb', '1-2.ipynb', '1-5.ipynb', '1-4.ipynb']

if we want to modify to find notebooks from any directory

import os

def find_notebooks(folderpath):
    files = os.listdir(folderpath) 
    notebooks = []
    for file in files:
        if file.endswith(".ipynb"):
            notebooks.append(file)
    return notebooks
find_notebooks()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[57], line 1
----> 1 find_notebooks()

TypeError: find_notebooks() missing 1 required positional argument: 'folderpath'
find_notebooks(os.getcwd())
['1-3.ipynb', '1-1.ipynb', '1-2.ipynb', '1-5.ipynb', '1-4.ipynb']
import os

def find_notebooks(folderpath=None):
    if folderpath is None:
        folderpath = os.getcwd()
        
    files = os.listdir(folderpath) 
    notebooks = []
    for file in files:
        if file.endswith(".ipynb"):
            notebooks.append(file)
    return notebooks
def find_notebooks(folderpath=os.getcwd()): # default value
        
    files = os.listdir(folderpath) 
    notebooks = []
    for file in files:
        if file.endswith(".ipynb"):
            notebooks.append(file)
    return notebooks
find_notebooks()
['1-3.ipynb', '1-1.ipynb', '1-2.ipynb', '1-5.ipynb', '1-4.ipynb']
find_notebooks("/opt/arcesium-python-2024-june/")
['1-3.ipynb', '1-1.ipynb', '1-2.ipynb', '1-5.ipynb', '1-4.ipynb']

Writing your own modules

Write a text file with extension .py in your favorite text editor. keep that file in current working directory

import hello
hello.say_hello("vikrant")
Hello vikrant
hello.greeting("vikrant")
Welcome vikrant
hello.greeting("vikrant")
Namaskar vikrant
hello.greeting("vikrant")
Welcome vikrant
hello.greeting("vikrant")
Welcome vikrant
hello.greeting("vikrant")
How do you do? vikrant
%%file hello1.py
import random 

def say_hello(name):
    print("Hello", name)


def greeting(name):
    greetings = ["Namaskar", "Welcome", "How do you do?", "Wannakam"]
    gr = random.choice(greetings)
    print(gr, name)
Writing hello1.py
import hello1 # you give only filename without extension..don't give .py
hello1.greeting("Arcesium")
Welcome Arcesium
%%file test.txt
some test text 
for testing out %%file
Writing test.txt
%%file test.py

def foo():
    pass
    
Overwriting test.py
import test # once imported python will never look at the changed file
%%file test.py

def foobar():
    pass
Overwriting test.py
import test
test.foo()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[91], line 1
----> 1 test.foo()

AttributeError: module 'test' has no attribute 'foo'
%%file testing.py

def foo():
    print("foo")
Writing testing.py
import testing
testing.foo()
foo
%%file testing.py

def foobar():
    print("foobar")
Overwriting testing.py
import testing # import happens only once
testing.foo()
foo
testing.foobar()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[106], line 1
----> 1 testing.foobar()

AttributeError: module 'testing' has no attribute 'foobar'

Writing python scripts/programs

%%file args.py
import sys

sys.argv # this is a list of strings
print(sys.argv)
Writing args.py
%%file add.py
import sys

thisfilename = sys.argv[0]
a = int(sys.argv[1])
b = int(sys.argv[2])
s = a + b # sum is computed 
print(s)
Writing add.py
!python add.py 42 50
92
!python add.py 56 57
113
%%file welcome.py
import sys

name = sys.argv[1]
print("Welcome", name)
Writing welcome.py
%%file welcome.py
import sys

def welcome(name):
    print("Welcome", name)

name = sys.argv[1]
welcome(name)
!python welcome.py vikrant
Welcome vikrant
  • You will have to call the program with python from command line
  • all the inputs to your program goes from command line arguments
  • all the command line arguments are text by default
  • first argument is always your program name

problem

Write a program mysum.py to sum multiple integers from commnad line

from jupyter prompt it will run like this

!python mysum.py 1 2 3 4 5 6 7 8 9 10
55
import sys 
sys.argv # these are the arguments with which this jupuyter program started
['/opt/tljh/user/lib/python3.10/site-packages/ipykernel_launcher.py',
 '-f',
 '/home/jupyter-pipal/.local/share/jupyter/runtime/kernel-b0c672a5-6c8d-4cdb-ba1d-3e63a06186fe.json']
nums = list(range(10))
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums[1:] # drop first item
[1, 2, 3, 4, 5, 6, 7, 8, 9]
# sys.argv[1:] # these will be number given by user from command line for our mysum.py program
%%file mysum.py
import sys # modules are imported at top

def to_ints(text_args):
    nums = []
    for t in text_args:
        nums.append(int(t))

    return nums
    
text_nums = sys.argv[1:]
nums = to_ints(text_nums)
print(sum(nums))
Writing mysum.py
!python mysum.py 1 2 3 4 5 6 7 8 9 10
55
%load_problem square
Problem: Square

Write a function square to compute the square of a number.

>>> square(4)
16

You can verify your solution using:

%verify_problem square

# your code here

def square(x):
    return x*x
square(5)
25
%verify_problem square
Found 2 checks
✓ verify square(2)
✓ verify square(4)
🎉 Congratulations! You have successfully solved problem square!!