Module II - Day 2

Python Foundation

Nov-Jan 2025 batch, Vikrant Patil

Date: 30 Dec 2025

Click here for All Notes

login to https://traininghub.vikrant.dev/ with your username and create a notebook with name module2-day2.ipynb

© Vikrant Patil

File handling

filepath = "/home/vikrant/programming/work/github/vikrant.dev/python-foundation-professionals/poem.txt"
with open(filepath) as f:
    print(f.read())
#print()
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!
filepath = "/home/vikrant/programming/work/github/vikrant.dev/python-foundation-professionals/poem.txt"
with open(filepath) as f:
    for line in f:
        print(line)
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!
filepath = "/home/vikrant/programming/work/github/vikrant.dev/python-foundation-professionals/poem.txt"
with open(filepath) as f:
    for line in f:
        print(line, end="")
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!
%%file data.csv
symbol,value,high value,low value,volume
APPLE,234.5,240.3,233.0,100
AT&T,221.6,22.5,220.0,200
IBM,125.7,127.3,123.0,50
NIKE,100.5,105.0,104.0,1000
Writing data.csv
def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        for line in csv:
            data.append(line.split(","))
        return data
read_csv("data.csv")
[['symbol', 'value', 'high value', 'low value', 'volume\n'],
 ['APPLE', '234.5', '240.3', '233.0', '100\n'],
 ['AT&T', '221.6', '22.5', '220.0', '200\n'],
 ['IBM', '125.7', '127.3', '123.0', '50\n'],
 ['NIKE', '100.5', '105.0', '104.0', '1000\n']]
def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        for line in csv:
            data.append(line.strip().split(","))
        return data
read_csv("data.csv")
[['symbol', 'value', 'high value', 'low value', 'volume'],
 ['APPLE', '234.5', '240.3', '233.0', '100'],
 ['AT&T', '221.6', '22.5', '220.0', '200'],
 ['IBM', '125.7', '127.3', '123.0', '50'],
 ['NIKE', '100.5', '105.0', '104.0', '1000']]
stocks = read_csv("data.csv")
stocks
[['symbol', 'value', 'high value', 'low value', 'volume'],
 ['APPLE', '234.5', '240.3', '233.0', '100'],
 ['AT&T', '221.6', '22.5', '220.0', '200'],
 ['IBM', '125.7', '127.3', '123.0', '50'],
 ['NIKE', '100.5', '105.0', '104.0', '1000']]
stocks[0]
['symbol', 'value', 'high value', 'low value', 'volume']
stocks[1]
['APPLE', '234.5', '240.3', '233.0', '100']
stocks[1][1]
'234.5'
def to_numeric(items, converters):
    numeric = []
    for item in items:
        
        
    return numeric

def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        for line in csv: # this will skip the line read before for loop
            linedata = line.strip().split(",")
            linedata_ = to_numeric(linedata, converters)
            data.append(linedata_)
        
        return data
read_csv("data.csv")
[['APPLE', '234.5', '240.3', '233.0', '100'],
 ['AT&T', '221.6', '22.5', '220.0', '200'],
 ['IBM', '125.7', '127.3', '123.0', '50'],
 ['NIKE', '100.5', '105.0', '104.0', '1000']]
converters = [str, float, float, float, float]
linedata = ['APPLE', '234.5', '240.3', '233.0', '100']

d = []
for i in range(len(linedata)):
    f = converters[i]
    v = f(linedata[i])
    d.append(v)
d
['APPLE', 234.5, 240.3, 233.0, 100.0]
finaldata = []
for f, d in zip(converters, linedata):
    finaldata.append(f(d))
finaldata
['APPLE', 234.5, 240.3, 233.0, 100.0]
X = ['x1', 'x2', 'x3', 'x4']
Y = ['y1', 'y2', 'y3']
Z = ['z1', 'z2', 'z3', 'z4']
for x, y, z in zip(X, Y, Z):
    print(x, y, z)
x1 y1 z1
x2 y2 z2
x3 y3 z3
def to_numeric(items, converters):
    numeric = []
    for f, d in zip(converters, items):
        numeric.append(f(d))
        
    return numeric

def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        for line in csv: # this will skip the line read before for loop
            linedata = line.strip().split(",")
            linedata_ = to_numeric(linedata, converters)
            data.append(linedata_)
        
        return data
read_csv("data.csv")
[['APPLE', 234.5, 240.3, 233.0, 100.0],
 ['AT&T', 221.6, 22.5, 220.0, 200.0],
 ['IBM', 125.7, 127.3, 123.0, 50.0],
 ['NIKE', 100.5, 105.0, 104.0, 1000.0]]
x, y = 10, 20
def to_numeric(items, converters):
    numeric = []
    for f, d in zip(converters, items):
        numeric.append(f(d))
        
    return numeric

def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        for line in csv: # this will skip the line read before for loop
            linedata = line.strip().split(",")
            linedata_ = to_numeric(linedata, converters)
            data.append(linedata_)
        
        return headers, data
colnames, data = read_csv("data.csv")
colnames
['symbol,value,high', 'value,low', 'value,volume']
data
[['APPLE', 234.5, 240.3, 233.0, 100.0],
 ['AT&T', 221.6, 22.5, 220.0, 200.0],
 ['IBM', 125.7, 127.3, 123.0, 50.0],
 ['NIKE', 100.5, 105.0, 104.0, 1000.0]]
def to_numeric(items, converters):
    numeric = []
    for f, d in zip(converters, items):
        numeric.append(f(d))
        
    return numeric

def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        for line in csv: # this will skip the line read before for loop
            linedata = line.strip().split(",")
            linedata_ = to_numeric(linedata, converters)
            data.append(linedata_)
        
        return headers, data
read_csv("data.csv")
(['symbol,value,high', 'value,low', 'value,volume'],
 [['APPLE', 234.5, 240.3, 233.0, 100.0],
  ['AT&T', 221.6, 22.5, 220.0, 200.0],
  ['IBM', 125.7, 127.3, 123.0, 50.0],
  ['NIKE', 100.5, 105.0, 104.0, 1000.0]])
read_csv("data.csv")
(['symbol,value,high', 'value,low', 'value,volume'],
 [['APPLE', 234.5, 240.3, 233.0, 100.0],
  ['AT&T', 221.6, 22.5, 220.0, 200.0],
  ['IBM', 125.7, 127.3, 123.0, 50.0],
  ['NIKE', 100.5, 105.0, 104.0, 1000.0]])

problem

Write a python script head.py which mimics unix command head. It should show first n lines of file passed as argument.

  !python3 head.py 5 poem.txt
  The Zen of Python, by Tim Peters

  Beautiful is better than ugly.
  Explicit is better than implicit.
  Simple is better than complex.

list comprehension

def to_numeric(items, converters):
    
    return [f(d) for f, d in zip(converters, items)]
        
    return numeric

def read_csv(filepath):
    with open(filepath) as csv:
        data = []
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        for line in csv: # this will skip the line read before for loop
            linedata = line.strip().split(",")
            linedata_ = to_numeric(linedata, converters)
            data.append(linedata_)
        
        return headers, data

read_csv("data.csv")
(['symbol,value,high', 'value,low', 'value,volume'],
 [['APPLE', 234.5, 240.3, 233.0, 100.0],
  ['AT&T', 221.6, 22.5, 220.0, 200.0],
  ['IBM', 125.7, 127.3, 123.0, 50.0],
  ['NIKE', 100.5, 105.0, 104.0, 1000.0]])
sqrs = []
for n in range(10):
    sqrs.append(n*n)
sqrs
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[n*n for n in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[do(item) for item in items]
def to_numeric(items, converters):
    return [f(d) for f, d in zip(converters, items)]

def process_line(line):
    linedata = line.strip().split(",")
    return to_numeric(linedata, converters)

def read_csv(filepath):
    with open(filepath) as csv:
        headers = csv.readline().strip().split()
        converters = [str, float, float, float, float]
        return headers, [process_line(line) for line  in csv]

read_csv("data.csv")
(['symbol,value,high', 'value,low', 'value,volume'],
 [['APPLE', 234.5, 240.3, 233.0, 100.0],
  ['AT&T', 221.6, 22.5, 220.0, 200.0],
  ['IBM', 125.7, 127.3, 123.0, 50.0],
  ['NIKE', 100.5, 105.0, 104.0, 1000.0]])
even_sqrs =  []
for i in range(20):
    if i%2 == 0:
        even_sqrs.append(i*i)
[i*i for i in range(20) if i%2==0]
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
insertdata ='TSLA',750.12,810.0,100,900.50
insertdata
('TSLA', 750.12, 810.0, 100, 900.5)
insertdata ="TSLA,750.12,810.0,100,900.50"
insertdata
'TSLA,750.12,810.0,100,900.50'
with open("data.csv", "a") as f:
    f.write(insertdata)
read_csv("data.csv")
(['symbol,value,high', 'value,low', 'value,volume'],
 [['APPLE', 234.5, 240.3, 233.0, 100.0],
  ['AT&T', 221.6, 22.5, 220.0, 200.0],
  ['IBM', 125.7, 127.3, 123.0, 50.0],
  ['NIKE', 100.5, 105.0, 104.0, 1000.0],
  ['TSLA', 750.12, 810.0, 100.0, 900.5]])
indexdata = [('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 indexdata:
    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)
[value for symbol, day, value in indexdata]
    
[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]
[value for symbol, day, value in indexdata if symbol=='APPLE']
[321.49182055844256,
 340.63612771662815,
 303.9065277507285,
 338.1350605764038,
 318.3912296144338]
[(symbol, value) for symbol, day, value in indexdata if day=="Monday"]
[('IBM', 111.71436961893693),
 ('MICROSOFT', 235.0403622499107),
 ('APPLE', 321.49182055844256)]
{symbol:value for symbol, day, value in indexdata if day=="Monday"}
{'IBM': 111.71436961893693,
 'MICROSOFT': 235.0403622499107,
 'APPLE': 321.49182055844256}

problem

  • find sum of all the numbers less than 1000 , divisible by 13 or 11
sum([])
sum([i for i in range(1000) if i%13==0 or i%11==0])
80080

problem

  • Write a function factors which finds all factors of given number (include 1 and self)
  • Write a function is_prime which checks if given number is prime based on fact that prime number has only two factors 1 and self.
  • Write a list comprehension to generate prime numbers less than 1000.
def factors(n):
    return [i for i in range(1, n+1) if n%i==0]
factors(10)
[1, 2, 5, 10]
factors(7)
[1, 7]
factors(13)
[1, 13]
factors(29)
[1, 29]
def is_prime(n):
    return factors(n) == [1, n]
is_prime(29)
True
is_prime(10)
False
[p for p in range(100) if is_prime(p)]
[2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
 23,
 29,
 31,
 37,
 41,
 43,
 47,
 53,
 59,
 61,
 67,
 71,
 73,
 79,
 83,
 89,
 97]