Apr 15-18, 2019 Vikrant Patil
These notes are available online at http://notes.pipal.in/2019/arcesium_basic_aug/day3.html
© Pipal Academy LLP
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
x = [1, 0, 0]
y = x
x = [1,1,1]
y
primes = [2,3,5,7,11,13,17,19]
primes[-1]#last element
primes[2:5]
primes[:3] #take first three
primes[3:] #drop frist three
primes[:] #everything
primes[1:5:2]
primes[::2]
primes[::-1]
primes[:-3] # drop last three
primes[-1:]
sentence = "Wizard of oz is python"
sentence[:5]
sentence[::-1]
pt = tuple(primes)
pt
pt[::-1]
primes[0] = 0
primes
primes[0] = 2
primes
pt[0] = 0
sentence[0] = "c"
sentence.count("o")
sentence.split() #split on white space
sentence = " "*5 + sentence + "\t"*5
sentence
sentence.split()
sentence
sentence.replace(" ", ",")
s = ' Wizard,of,oz,is,python\t\t\t\t\t'
s.split(",")
s.strip().split(",")
Question
filename = "hello.exe"
Find extension?
filename= "hello.exe"
filename.split(".")
filename.split()[-1] # xtension
value = "101 Rs"
value, currency = value.split()
value
currency
value = float(value)
value
primes
[x*x for x in primes] # square each item in the list
[e for e in primes if e%2==0]
[i for i in range(1, 1000) if i%7==0 or i%13==0]
def multiplesof13or7(n):
return [i for i in range(1, n) if i%7==0 or i%13==0]
multiplesof13or7(100)
tables = [[i*n for i in range(1, 11)] for n in range(1,6)]
tables
tables[0] ##0th row
tables[-1] #last row
tables[0]
tables[0][0]
tables[0][1]
tables[0][2]
tables[0][3]
tables[0][4]
lets access last column (9th index column)
tables
tables[0][9]
tables[1][9]
tables[2][9]
tables[3][9]
tables[4][9]
def column(data, colnum):
return [ data[i][colnum] for i in range(len(data))]
column(tables, 9)
column(tables, 1)
tables
for row in tables:
print(row)
def column(data, colnum):
return [row[colnum] for row in data]
column(tables, 0)
column(tables, 4)
def transpose(data):
columncount = len(data[0])
return [column(data, i) for i in range(columncount)]
transpose(tables)
td = transpose(tables)
td[0]
td[-1]
for p in primes:
print(p, end=",")
for p in reversed(primes):
print(p, end=",")
reversed(primes)
rprimes = reversed(primes) # should not do this
for p in rprimes:
print(p, end=",")
for p in rprimes:
print(p, end=",")
rp = primes[::-1]
rp
for i, p in enumerate(primes):
print(i, p)
first = ["Elsa", "Alice", "Elisa"]
second = ["Frozen", "Wonder", "Hacker"]
for name, surname in zip(first, second):
print(name, surname)
def add(x,y):
return x+y
add(2,3)
pair = [2, 3]
add(pair[0], pair[1])
add(*pair)
zip(*tables)
list(zip(*tables))
problem
vector_add which does vector addition of two vectors (taken as list or tuple)list(zip("hello", "there"))
v1 = [1, 2, 3, 4, 5]
v2 = [1]*5
[x+y for x,y in zip(v1, v2)]
import random
values = [random.random() for i in range(10)]
currency = [random.choice(["Rs","$","E"]) for i in range(10) ]
values
currency
for v,c in zip(values, currency):
print(v, c)
[str(v) + " " + c for v,c in zip(values, currency)]
list(zip(values, currency))
l = [1,2,3, [1,2,3]]
for item in l:
print(item)
problems
tickers = "hcl ,infy , tata,rel,simls"
tickers.split(",")
[item.strip() for item in tickers.split(",")]
list(tickers)
nums = [42, 34, 35,35, 232,42, 34, 23, 45, 23, 45, 11, 34, 23, 11]
def removeduplicates(nums):
uniq = []
for n in nums:
if n not in uniq:
uniq.append(n)
return uniq
removeduplicates(nums)
list(set(nums))
"{desg} of oz is {who}".format(desg="Wizard", who="Python")
template = "{0} did something called {1}, which resulted in {2}"
template.format("Alice", "sleeping", "dream")
template = "{1} did something called {0}, which resulted in {2}"
template.format("Alice", "sleeping", "dream")
"{} did something called {}, which resulted in {}".format("x","y","z")
"{} did something called {}, which resulted in {}".format("x","z","y")
"{} did something called {}, which resulted in {}".format("x","z")
"{} did something called {}, which resulted in {}".format("x","z","p","q")
import this
%%file poem.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!
with open("poem.txt") as f:
print(f.read())
with open("poem.txt") as f:
data = f.read()
print(data.replace("\n", " "))
f = open("poem.txt")
f.readline()
f.readline()
f.readline()
f.read()
f.read()
f.close()
with open("poem.txt") as f:
for line in f:
print(line, end="")
with open("poem.txt") as f:
for i, line in enumerate(f):
print(i+1, line, end="")
with open("poem.txt") as f:
for i, line in enumerate(f):
print(i+1, line)
with open("poem.txt") as f:
for line in reversed(f.readlines()):
print(line, end="")
problems
head.py which takes filename and number of lines as argument and prints only those many linestail.py which takes filename and number of lines as argument as prints last lines.!head poem.txt
%%file head.py
import sys
def head(filename, n):
with open(filename) as f:
for i in range(n):
print(f.readline(), end="")
if __name__ == "__main__":
filename = sys.argv[1]
n = int(sys.argv[2])
head(filename, n)
!python head.py poem.txt 4
%%file tail.py
import sys
def tail(filename, n):
with open(filename) as f:
lines = f.readlines()
for line in lines[-n:]:
print(line, end="")
if __name__ == "__main__":
filename = sys.argv[1]
n = int(sys.argv[2])
tail(filename, n)
!python tail.py poem.txt 5
from collections import deque
dq = deque(maxlen=3)
dq.append(1)
dq.append(2)
dq.append(3)
dq
dq.append(4)
dq
%%file tail1.py
import sys
from collections import deque
def tail(filename, n):
with open(filename) as f:
window = deque(maxlen=n)
for line in f:
window.append(line)
for line in window:
print(line, end="")
if __name__ == "__main__":
filename = sys.argv[1]
n = int(sys.argv[2])
tail(filename, n)
!python tail1.py poem.txt 5
with open("data.txt", "w") as f:
f.write("one")
f.write("\n")
f.write("two\n")
f.write("three")
!python head.py data.txt 3
%%file cat.py
import sys
if __name__ == "__main__":
with open(sys.argv[1]) as f:
print(f.read())
!python cat.py data.txt
words = ["one", "two", "three", "four", "five", "six"]
with open("numbers.txt", "w") as f:
for w in words:
f.write(w)
f.write("\n")
!python cat.py numbers.txt
with open("numbers.txt", "a") as f:
f.write("seven\n")
!python cat.py numbers.txt
words
",".join(words)
tables
def writecsv(data, filename):
with open(filename, "w") as f:
for row in data:
strnums = [str(i) for i in row]
line = ",".join(strnums)
f.write(line)
f.write("\n")
writecsv(tables, "tables.csv")
!python cat.py tables.csv
line
def parsecsv(filename):
with open(filename) as f:
data = []
for line in f:
data.append(line.strip().split(","))
return data
parsecsv("tables.csv")
s = []
for i in nums:
s.append(i*i)
def intlist(strnums):
return [int(s) for s in strnums]
def parsecsv(filename):
with open(filename) as f:
data = []
for line in f:
strnums = line.strip().split(",")
data.append(intlist(strnums))
return data
parsecsv("tables.csv")
int("dsdsd")
int("[1,2,3,4]")
int(["1","2"])
int("23")
n = ["1","2","3","4"]
[int(i) for i in n]
Different modes for reading/writing files
'r' -> read
'w' -> write
'a' - > append
'b' -> binary
'rb' -> read binary
'ab' -> append binary
'wb' -> write brinary
if you don't specify b mode , it means default mode is text file.
machine = {
"cpu" : "corei3",
"memeory": "4GB",
"os": "windows",
"storage" : "512GB",
"graphics" : "Nvidia"
}
machine
for item in machine:
print(item, machine[item])
for item in machine:
print(item.ljust(8), machine[item])
for k, v in machine.items():
print(k.ljust(8), v)
machine['cpu']
machine.get('cpu')
machine['name']
machine.get('name',"mozart")
machine
del machine['cpu']
machine
machine['cpu'] = 'corei5'
machine
for v in machine.values():
print(v)
for k in machine.keys():
print(k)
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three four five six
one two three four five six seven
one two three four five six seven eight
one two three four five six seven eight nine
one two three four five six seven eight nine ten
def getwords(filename):
with open(filename) as f:
return f.read().strip().split()
words = getwords("words.txt")
def wordfreq(words):
freq = {}
uniq = set(words)
for w in uniq:
freq[w] = words.count(w)
return freq
freq = wordfreq(words)
freq
sorted(freq)
def get_freq(row):
return row[1]
for k,v in sorted(freq.items(), key=get_freq):
print(k, v)
for k,v in sorted(freq.items(), key=get_freq):
print(k.ljust(5), v)
for k,v in sorted(freq.items(), key=get_freq, reverse=True):
print(k.ljust(5), v)
for k,v in sorted(freq.items(), key=get_freq, reverse=True):
print(k.rjust(5), str(v).ljust(2), "*"*v)