Module II - Practice

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 22 Nov 2025

Click here for All Notes

© Vikrant Patil

methods

text = "This is random text for practice"
text.split() # by default splits on spaces
['This', 'is', 'random', 'text', 'for', 'practice']
comma_seprated= "column1,column2,column3,column4"
comma_seprated.split(",")
['column1', 'column2', 'column3', 'column4']
filename = "hello.py"
filename.split(".")
['hello', 'py']
nums = [1, 2, 3, 4]
nums[0]
1
nums[1]
2
nums[3]
4
nums[-1]
4
filename.split(".")[-1]
'py'
def find_extension(filename):
    return filename.split(".")[-1]
find_extension("Zen.txt")
'txt'
find_extension("module2-practice.ipynb")
'ipynb'
words = ["one", "two", "three", "four", "five"]
"-".join(words)
'one-two-three-four-five'
" ".join(words)
'one two three four five'
"".join(words)
'onetwothreefourfive'
import os
os.path.sep.join([ "", "home", "vikrant"])
'/home/vikrant'
"/".join(["", "home", "vikrant"])
'/home/vikrant'
math
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 math

NameError: name 'math' is not defined
import math # import module called math
math
<module 'math' (built-in)>
import math2
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[32], line 1
----> 1 import math2

ModuleNotFoundError: No module named 'math2'
math.sqrt(5)
2.23606797749979
math.e
2.718281828459045
math.pi
3.141592653589793
"\n"
'\n'
"\""
'"'
"\\"
'\\'
"\t"
'\t'
empty = []
empty.append(0)
empty
[0]
empty.append(0)
empty
[0, 0]
!ls
about.qmd~      module1-day5.html       students-module1-day2.html
args1.py        module1-day5.ipynb      students-module1-day2.ipynb
cat.py          module2-day1.ipynb      students-module1-day3.ipynb
chaos1.py       module2-day3.ipynb      students-module1-day4.html
chaos.py        module2-practice.html   students-module1-day4.ipynb
copyzen.txt     module2-practice.ipynb  students-module2-day2.html
head.py         notes.html          students-module2-day2.ipynb
hello_person.py     notes.qmd           styles.css
hello.py        notes.qmd~          tail.py
helloworld.py       poem.txt            test_args.py
index.html      power.py            testargs.py
index.qmd       problems.ipynb      test_prog.py
index.qmd~      promotions          today.org
Makefile        push            topics.qmd
Makefile~       __pycache__         topics.qmd~
maths_test_new.py   _quarto.yml         trainer.html
maths_test.py       _quarto.yml~        trainer.qmd
module1-day1_files  revision.ipynb      trainer.qmd~
module1-day1.ipynb  say_hello.py        welcome_multi_lingual.py
module1-day2.ipynb  _site           x.txt
module1-day3.ipynb  site_libs           zen.txt
module1-day4.ipynb  square.py
!touch x.txt y.txt z.txt a.pdf b.pdf c.pdf m.html n.html l.html
files = "x.txt y.txt z.txt a.pdf b.pdf c.pdf m.html n.html l.html".split()
files
['x.txt',
 'y.txt',
 'z.txt',
 'a.pdf',
 'b.pdf',
 'c.pdf',
 'm.html',
 'n.html',
 'l.html']
for f in files:
    print(f)
x.txt
y.txt
z.txt
a.pdf
b.pdf
c.pdf
m.html
n.html
l.html
txtfiles = []
for f in files:
    if find_extension(f) == "txt":
        txtfiles.append(f)
txtfiles
['x.txt', 'y.txt', 'z.txt']
txtfiles = []
pdffiles = []
htmlfiles = []
for f in files:
    if f.endswith(".txt"):
        txtfiles.append(f)
    elif f.endswith(".pdf"):
        pdffiles.append(f)
    elif f.endswith(".html"):
        htmlfiles.append(f)
txtfiles
['x.txt', 'y.txt', 'z.txt']
pdffiles
['a.pdf', 'b.pdf', 'c.pdf']
htmlfiles
['m.html', 'n.html', 'l.html']

list slicing

files
['x.txt',
 'y.txt',
 'z.txt',
 'a.pdf',
 'b.pdf',
 'c.pdf',
 'm.html',
 'n.html',
 'l.html']
files[0]
'x.txt'
files[1]
'y.txt'
files[-1]
'l.html'
files[2:6] # start at index 2 and take till index 5
['z.txt', 'a.pdf', 'b.pdf', 'c.pdf']
nums = list(range(10))
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums[0]
0
nums[2:8]
[2, 3, 4, 5, 6, 7]
nums[1:8:2] # start at index 1, end at index 7 at stept of 2
[1, 3, 5, 7]
nums[:5]  # start at 0 and end at index 4
[0, 1, 2, 3, 4]
nums[:5] # take first 5
[0, 1, 2, 3, 4]
nums[:3] # take first 3
[0, 1, 2]
nums[3:] # drop first three
[3, 4, 5, 6, 7, 8, 9]
nums[::] # copy
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
copy_nums = nums[::]
copy_nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums[::-1] # reverse the list
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  • nums[start:end:step]
  • whatever you don’t give, a default value will be taken
  • default for start is 0
  • default for end is end of list
  • defautl for step is 1
[1, 2, 3] + [2, 3, 4] # concatenation
[1, 2, 3, 2, 3, 4]
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums.extend([2, 3, 4])
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4]
"text" * 3
'texttexttext'
[1, 2, 3] * 4
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
"hello" in text
False
"random" in text
True

problem Write a function timebomb which prints “tick” every second and after n seconds prints “BOOOM!!!!” n is integer argument o function. Make use time module. e.g. time.sleep(2) function waits for 2 seconds.

def timebomb(n):
    pass
import time
time.sleep(2)
time.sleep(5)
print("hello")
hello
import time

def timebomb(n):
    for i in range(n):
        time.sleep(1)        
        print("tick")

    print("BOOOOOM!")
timebomb(5)
tick
tick
tick
tick
tick
BOOOOOM!
for i in range(10):
    print(i)

print("Done")
0
1
2
3
4
5
6
7
8
9
Done
sum(nums)
54
max(nums)
9
min(nums)
0
len(nums)
13
len(text)
32

scripts

%%file zen.txt
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Overwriting zen.txt
%%file hello.py

print("Hello")
Overwriting hello.py
%%file args1.py
import sys

print(sys.argv)
Overwriting args1.py
%%file hello_person.py
import sys

name = sys.argv[1]
print("Hello", name)
Overwriting hello_person.py