Basic Python Training at Arcesium - Day 3

Nov 26-30, 2018 Vikrant Patil

These notes are available online at http://notes.pipal.in/2018/arcesium-basic-nov/day3.html

© Pipal Academy LLP

Day 1 | Day 2 | Day 3 | Day 4 | Day 5

We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from

https://www.anaconda.com/download/

modules and scripts

In [1]:
%%file foo.py

def bar():
    print("foobar!")
    
    
bar()
Writing foo.py
In [2]:
import foo
foobar!
In [ ]:
 
In [4]:
%%file backup.py

def download_email():
    print("downloading email!")
    
    
def copy_new_delete_old(new, old):
    print("copying new and deleting old ones")

    
def backup():
    download_email()
    #....(
    #...
    src = "src"
    dest = "dest"
    copy_new_delete_old(src, dest)
    
print(__name__)
#backup()
Overwriting backup.py
In [5]:
import backup
backup
In [6]:
!python backup.py
__main__
In [7]:
%%file backup1.py

def download_email():
    print("downloading email!")
    
    
def copy_new_delete_old(new, old):
    print("copying new and deleting old ones")

    
def backup():
    download_email()
    #....(
    #...
    src = "src"
    dest = "dest"
    copy_new_delete_old(src, dest)
    
if __name__ == "__main__":
    backup()
Writing backup1.py
In [8]:
!python backup1.py
downloading email!
copying new and deleting old ones
In [9]:
import backup1
In [10]:
def sumofsquares(x,y):
    return square(x) + square(y)
    
In [11]:
sumofsquares(3,4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-4b7eac176dd0> in <module>()
----> 1 sumofsquares(3,4)

<ipython-input-10-e72dffdb4e15> in sumofsquares(x, y)
      1 def sumofsquares(x,y):
----> 2     return square(x) + square(y)
      3 

NameError: name 'square' is not defined
In [12]:
def square(x):
    return x*x
In [13]:
sumofsquares(4,5)
Out[13]:
41
In [14]:
def square(x):
    return x*x
def sumofsquares(x,y):
    return square(x) + square(y)
In [15]:
def sumofcubes(x,y):
    def cube(x):
        return x**3
    
    return cube(x) + cube(y)
In [16]:
sumofcubes(2, 3)
Out[16]:
35
In [17]:
square(4)
Out[17]:
16
In [18]:
cube(4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-44fb567fb95d> in <module>()
----> 1 cube(4)

NameError: name 'cube' is not defined
In [20]:
sumofcubes(4,5)
Out[20]:
189
In [21]:
%%file square.py
import sys

def square(x):
    return x*x


if __name__ == "__main__":
    a = float(sys.argv[1])
    print(square(a))
Overwriting square.py
In [22]:
!python square.py 6
36.0
In [23]:
import square
In [24]:
square.square(34)
Out[24]:
1156
In [25]:
%%file square1.py

def square(x):
    return x*x


print(square(3))
Overwriting square1.py
In [26]:
!python square1.py 5
9
In [27]:
import square1
9
In [30]:
%%file square2.py
import sys
def square(x):
    return x*x


print(square(float(sys.argv[1])))
Overwriting square2.py
In [31]:
!python square2.py 6
36.0
In [32]:
import square2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-74f8980bc796> in <module>()
----> 1 import square2

~/trainings/2018/arcesium-basic-nov/square2.py in <module>()
      4 
      5 
----> 6 print(square(float(sys.argv[1])))

ValueError: could not convert string to float: '-f'

loops

In [35]:
def evens(n):
    i = 0
    while i <= n:
        if i%2 == 0:
            print(i)
        i = i + 1
        #i += 1
    
In [36]:
evens(10)
0
2
4
6
8
10
In [37]:
def evens(n):
    i = 0
    while i <= n:
        if i%2 == 0:
            print(i, end=" ")
        i = i + 1
        #i += 
In [38]:
evens(20)
0 2 4 6 8 10 12 14 16 18 20 
In [40]:
def odds(n):
    while j <= n:
        if j%2 == 1:
            print(j, end=" ")
        j = j + 1
        #i += 
In [41]:
odds(10)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-41-1109a5763a03> in <module>()
----> 1 odds(10)

<ipython-input-40-bcc1541ad8b8> in odds(n)
      1 def odds(n):
----> 2     while j <= n:
      3         if j%2 == 1:
      4             print(j, end=" ")
      5         j = j + 1

UnboundLocalError: local variable 'j' referenced before assignment
In [42]:
from math import pi
In [43]:
pi
Out[43]:
3.141592653589793
In [44]:
pi = 5
In [45]:
pi
Out[45]:
5
In [46]:
import math
In [47]:
math.pi
Out[47]:
3.141592653589793
In [48]:
a = 3
def fun(x):
    return x+a
In [49]:
fun(2)
Out[49]:
5
In [50]:
def foo():
    a = 7
In [51]:
foo()
In [52]:
a
Out[52]:
3
In [53]:
a = 42
In [54]:
fun(2)
Out[54]:
44
In [66]:
j = 10

def odds(n):
    global j
    while j <= n:
        if j%2 == 1:
            print(j, end=" ")
        j = j + 1
        #i += 
In [67]:
odds(30)
11 13 15 17 19 21 23 25 27 29 
In [69]:
print(j)
31
In [ ]:
 

break

In [70]:
def odds(n):
    i = 0
    while True:
        if i%2 == 1:
            print(i)
        if i == n:
            break
        i = i + 1
In [71]:
odds(10)
1
3
5
7
9
In [72]:
import time
while True:
    time.sleep(2)
    print("hello")
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-72-6fdc2fbb671a> in <module>()
      1 import time
      2 while True:
----> 3     time.sleep(2)
      4     print("hello")

KeyboardInterrupt: 

for loop

In [73]:
numbers = [1,2,3,4,5,6,7]
for num in numbers:
    print(num,end=",")
1,2,3,4,5,6,7,
In [74]:
numbers
Out[74]:
[1, 2, 3, 4, 5, 6, 7]
In [76]:
x in numbers
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-76-9e50b2b3e0b3> in <module>()
----> 1 x in numbers

NameError: name 'x' is not defined
In [77]:
for x in numbers:
    x2 = 2*x
    print(x2, end=",")
2,4,6,8,10,12,14,
In [78]:
for word in ["one","two","three","four"]:
    print(word)
one
two
three
four
In [80]:
for c in "This is a string for testing for loop":
    print(c, end=",")
T,h,i,s, ,i,s, ,a, ,s,t,r,i,n,g, ,f,o,r, ,t,e,s,t,i,n,g, ,f,o,r, ,l,o,o,p,
In [81]:
for item in {"x":1,"y":2, "z":3}:
    print(item)
x
y
z
In [82]:
d = {"x":1,"y":2, "z":3}
for item in d :
    print(item, d[item])
x 1
y 2
z 3
In [83]:
s = "This is a string for testing for loop"
for word in s.split():
    print(word, end=",")
This,is,a,string,for,testing,for,loop,
In [84]:
empty= []
In [85]:
def twicelist(numbers):
    twice = []
    for n in numbers:
        twice.append(2*n)
    return twice
In [86]:
twicelist([1,4,2,3,1,2])
Out[86]:
[2, 8, 4, 6, 2, 4]
In [92]:
def twice_half_list(numbers):
    size = len(numbers)
    return twicelist(numbers[:size//2])
In [93]:
twice_half_list([1,2,3,4,5,6,7,8])
Out[93]:
[2, 4, 6, 8]

problem

  • Given list of numbers in string format, convert them to list of floats
    >>> convert_float(['1','2','3','4'])
    [1.0,2.0,3.0,4.0]
In [94]:
s = "2.3"
In [95]:
s
Out[95]:
'2.3'
In [96]:
s/2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-96-5306df4da785> in <module>()
----> 1 s/2

TypeError: unsupported operand type(s) for /: 'str' and 'int'
In [98]:
float(s)/2
Out[98]:
1.15

problem

  • Write a script add.py which can add numbers taken from commandline
In [99]:
%%file add.py
import sys

def convert_float(strnums):
    nums = []
    for s in strnums:
        nums.append(float(s))
        
    return nums

if __name__ == "__main__":
    numbers = convert_float(sys.argv[1:])
    print(sum(numbers))
Writing add.py
In [100]:
!python add.py 1 2 3 4 5 -6
9.0
In [101]:
def minimum(x,y):
    if x < y:
        return x
    else:
        return y
In [102]:
def minimum3(x,y,z):
    m = minimum(x,y)
    return minimum(z, m)
In [103]:
minimum3(1,2,5)
Out[103]:
1
In [104]:
def minimum3_(x,y,z):
    if x < y:
        if x < z:
            return x
        else:
            return z
    else:
        if y < z:
            return y
        else:
            return z

list comprehensions

In [107]:
def even(x):
    return x%2==0

def square_evens(numbers):
    sqrs = []
    for n in numbers:
        if even(n):
            sqrs.append(n*n)
    return sqrs
In [108]:
square_evens(range(20))
Out[108]:
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
for item in seq:
    do(item)      =>  [do(item) for item in seq]
for item in seq:
    if cond:
       do(item)   => [do(item) for item in seq if cond]
In [109]:
sq = []
for i in range(10):
    sq.append(i*i)
In [110]:
[i*i for i in range(10)]
Out[110]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [111]:
sq = []
for i in range(10):
    if even(i):
        sq.append(i*i)
In [112]:
sq
Out[112]:
[0, 4, 16, 36, 64]
In [113]:
[i*i for i in range(10) if even(i)]
Out[113]:
[0, 4, 16, 36, 64]
In [115]:
digits = list(range(10))
In [117]:
[i*i for i in digits[::3]]
Out[117]:
[0, 9, 36, 81]
In [119]:
x = [i*i for i in digits]
In [120]:
x
Out[120]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

problems Write lists comprehensions for following

  • Find even numbers from given list
  • find only files from given directory
  • find only directories from given directory
  • find pyhon files form given directory
  • find sum of all multiples of 7 or 11 less than 1000
In [121]:
numbers = [1,3,2,5,3,4,8,12,8,10]
In [122]:
[ i for i in numbers if even(i)]
Out[122]:
[2, 4, 8, 12, 8, 10]
In [138]:
import os
def list_files(dirpath):
    files = os.listdir(dirpath)  
    return [f for f in files if os.path.isfile(os.path.join(dirpath, f))]
In [139]:
binfiles = list_files("/bin/")
In [140]:
len(binfiles)
Out[140]:
176
In [141]:
os.path.isfile("echo")
Out[141]:
False
In [142]:
os.path.isfile("/bin/echo")
Out[142]:
True
In [143]:
binfiles[:3]
Out[143]:
['bzfgrep', 'nano', 'dir']
In [1]:
sum([i for i in range(1000) if i%7==1 or i%11==1])
Out[1]:
110331

bonus problem

  • write list comprehension to find all factors of given integer
    >>> factors(10)
    [1,2,5,10]
  • make use of above function to write a function to check if given number is prime or not
    >>> is_prime(5)
    True
In [2]:
def factors(n):
    return [i for i in range(1, n+1) if n%i==0]
In [3]:
factors(8)
Out[3]:
[1, 2, 4, 8]
In [4]:
factors(5)
Out[4]:
[1, 5]
In [5]:
def is_prime(n):
    return len(factors(n))==2
In [6]:
is_prime(5)
Out[6]:
True
In [7]:
is_prime(50)
Out[7]:
False
In [8]:
def primes(n):
    return [p for p in range(1,n+1) if is_prime(p)]
In [10]:
primes(50)
Out[10]:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

nested lists

In [11]:
matrix =[[1,2,3],
         [4,5,6],
         [7,8,9]]
In [12]:
matrix
Out[12]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [13]:
matrix[0]
Out[13]:
[1, 2, 3]
In [14]:
matrix[-1]
Out[14]:
[7, 8, 9]
In [15]:
matrix[0][0]
Out[15]:
1
In [19]:
oned = [i for i in range(1,50+1) if is_prime(i)]
In [20]:
oned
Out[20]:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
In [25]:
tables = [[i*j for i in range(1,11)] for j in range(1,6)]
In [26]:
tables
Out[26]:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
 [3, 6, 9, 12, 15, 18, 21, 24, 27, 30],
 [4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]]
In [27]:
t = []
for j in range(1,6):
    tr = []
    for i in range(1,11):
        tr.append(i*j)
    t.append(tr)
In [28]:
t
Out[28]:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
 [3, 6, 9, 12, 15, 18, 21, 24, 27, 30],
 [4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]]
In [29]:
for row in tables:
    print(sum(row), end=" ")
55 110 165 220 275 

looping patterns

In [30]:
p = primes(50)
In [31]:
p
Out[31]:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
In [32]:
for i in p:
    print(i, end=" ")
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
In [34]:
for i in reversed(p):
    print(i, end=" ")
47 43 41 37 31 29 23 19 17 13 11 7 5 3 2 
In [35]:
p[::-1]
Out[35]:
[47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
In [36]:
x = reversed(p)
In [37]:
for i in x:
    print(i, end=",")
47,43,41,37,31,29,23,19,17,13,11,7,5,3,2,
In [39]:
for j in x:
    print(i, end=",")
In [40]:
p
Out[40]:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
In [41]:
reversed(p)
Out[41]:
<list_reverseiterator at 0x7fd9c449b780>
In [42]:
for i,x in enumerate(p):
    print(i, x)
0 2
1 3
2 5
3 7
4 11
5 13
6 17
7 19
8 23
9 29
10 31
11 37
12 41
13 43
14 47
In [43]:
[i for i in reversed(p)]
Out[43]:
[47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
In [44]:
[(index, item) for index,item in enumerate(["A","B","C"])]
Out[44]:
[(0, 'A'), (1, 'B'), (2, 'C')]
In [45]:
names = ["Alice", "David", "Elsa", "Oz"]
surnames  = ["Wonder", "Beazly", "Frozen", "Wizard"]
In [49]:
for n,s in zip(names, surnames): #join two lists side by side
    print(n, s)
Alice Wonder
David Beazly
Elsa Frozen
Oz Wizard
In [47]:
[(index, item) for index,item in enumerate(["A","B","C"])]
Out[47]:
[(0, 'A'), (1, 'B'), (2, 'C')]
In [48]:
[(index, item) for index,item in zip(range(3), ["A","B","C"])]
Out[48]:
[(0, 'A'), (1, 'B'), (2, 'C')]

Working with files

In [50]:
f = open("/home/vikrant/trainings/2018/arcesium-basic-nov/poem.txt")
In [51]:
contents = f.read()
In [54]:
print(contents[:100])
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
S
In [53]:
f.close()
In [55]:
with open("poem.txt") as fd:
    print(fd.read()[:200])
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

creating files

In [56]:
import os
In [57]:
os.mkdir("txtdata")
In [58]:
with open("x.txt", "w") as txt:
    txt.write("one")
    txt.write("\n")
    txt.write(contents)
    
In [67]:
with open("x.txt", "r") as txt:
    print(txt.read()[:100])
one
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implici
In [60]:
def writefiles(data, folder):
    name = "textdata"
    for i in range(100):
        fname = name + str(i) + ".txt"
        fpath = os.path.join(folder, fname)
        with open(fpath, "w") as f:
            f.write(data)
            
In [62]:
writefiles(contents, "txtdata")
In [63]:
files = os.listdir("txtdata/")
In [64]:
len(files)
Out[64]:
100
In [66]:
sorted(files)[:10]
Out[66]:
['textdata0.txt',
 'textdata1.txt',
 'textdata10.txt',
 'textdata11.txt',
 'textdata12.txt',
 'textdata13.txt',
 'textdata14.txt',
 'textdata15.txt',
 'textdata16.txt',
 'textdata17.txt']

apending to file

In [69]:
with open("numbers.txt", "w") as f:
    f.write("one\n")
    f.write("two\n")
    f.write("three\n")
In [70]:
with open("numbers.txt", "a") as f:
    f.write("four\n")
    f.write("five\n")
In [72]:
print(open("numbers.txt").read())
one
two
three
four
five

In [73]:
with open("binary.bin", "wb") as b:
    b.write(b"hello world!")
In [74]:
print(open("binary.bin", "rb").read())
b'hello world!'
In [75]:
with open("poem.txt") 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!

In [76]:
"I have this sentence, with some, commas , in it".split(",")
Out[76]:
['I have this sentence', ' with some', ' commas ', ' in it']
In [77]:
data = [["A","B","C","D"],
        ["A1","B1","C1","D1"],
        ["A2","B2","C2","D2"],
        ["A3","B3","C3","D3"],
        ["A4","B4","C4","D4"]]
In [78]:
data[0]
Out[78]:
['A', 'B', 'C', 'D']
In [79]:
",".join(data[0])
Out[79]:
'A,B,C,D'
In [84]:
def writecsv(data, filename):
    with open(filename, "w") as f:
        for row in data:
            f.write(",".join(row))
            f.write("\n")
    
In [85]:
writecsv(data, "data.csv")
In [86]:
def cat(filename):
    with open(filename) as f:
        print(f.read())
In [87]:
cat("data.csv")
A,B,C,D
A1,B1,C1,D1
A2,B2,C2,D2
A3,B3,C3,D3
A4,B4,C4,D4

In [113]:
def head(filename, n):
    with open(filename) as f:
        count = 0
        while count < n:
            line = f.readline()
            print(line, end="")
            count += 1
            
            
In [96]:
head("poem.txt", 4)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
In [97]:
head("data.csv", 2)
A,B,C,D
A1,B1,C1,D1
In [98]:
def head_(filename, n):
    with open(filename) as f:
        for i in range(n):
            print(f.readline(), end="")
            
In [100]:
f = open("data.csv")
In [101]:
f.read() # reads compete file as single string
Out[101]:
'A,B,C,D\nA1,B1,C1,D1\nA2,B2,C2,D2\nA3,B3,C3,D3\nA4,B4,C4,D4\n'
In [102]:
f.close()
In [103]:
f = open("data.csv")
In [104]:
f.readline()
Out[104]:
'A,B,C,D\n'
In [105]:
f.readline()
Out[105]:
'A1,B1,C1,D1\n'
In [106]:
f.readline()
Out[106]:
'A2,B2,C2,D2\n'
In [107]:
f.readline()
Out[107]:
'A3,B3,C3,D3\n'
In [108]:
f.readline()
Out[108]:
'A4,B4,C4,D4\n'
In [109]:
f.readline()
Out[109]:
''
In [110]:
f.readline()
Out[110]:
''
In [111]:
f.read()
Out[111]:
''
In [112]:
f.close()
In [114]:
f = open("data.csv")
lines = f.readlines()
In [115]:
lines
Out[115]:
['A,B,C,D\n',
 'A1,B1,C1,D1\n',
 'A2,B2,C2,D2\n',
 'A3,B3,C3,D3\n',
 'A4,B4,C4,D4\n']
In [116]:
def grep(patternstring, filename):
    with open(filename) as f:
        for line in f:
            if patternstring in line:
                print(line, end="")
In [118]:
grep("Errors", "poem.txt")
Errors should never pass silently.
In [125]:
def grep(patternstring, filename):
    with open(filename) as f:
        for i, line in enumerate(f):
            if patternstring in line:
                print(i+1, line, end="")
In [126]:
grep("Errors", "poem.txt")
12 Errors should never pass silently.
In [127]:
grep("def ", "wc.py")
6 def wordcount(text):
13 def linecount(text):
16 def charcount(text):
In [128]:
def head(filename, n):
    with open(filename) as f:
        count = 0
        while count < n:
            line = f.readline()
            print(line, end="")
            count += 1
In [146]:
%%file stocks.csv
name,ticker,value,volume
Infosys,infy,1000,500
Tata,tata,500,50
Reliance,reliance,700,100
Tata Infotech,tatainf,600,60
Overwriting stocks.csv
In [147]:
def readcsv(filename):
    with open(filename) as f:
        data = []
        for line in f:
            data.append(line.strip().split(","))
        return data
In [148]:
readcsv("stocks.csv")
Out[148]:
[['name', 'ticker', 'value', 'volume'],
 ['Infosys', 'infy', '1000', '500'],
 ['Tata', 'tata', '500', '50'],
 ['Reliance', 'reliance', '700', '100'],
 ['Tata Infotech', 'tatainf', '600', '60']]
In [149]:
def readcsv_(filename):
    with open(filename) as f:
        return [line.strip().split(",") for line in f]
In [150]:
readcsv_("stocks.csv")
Out[150]:
[['name', 'ticker', 'value', 'volume'],
 ['Infosys', 'infy', '1000', '500'],
 ['Tata', 'tata', '500', '50'],
 ['Reliance', 'reliance', '700', '100'],
 ['Tata Infotech', 'tatainf', '600', '60']]
In [151]:
def grep1(patternstr, filename):
    with open(filename) as f:
        return [line for line in f if patternstr in line]
In [152]:
grep1("Errors", "poem.txt")
Out[152]:
['Errors should never pass silently.\n']
In [153]:
lines = grep1("Tata", "stocks.csv")
In [154]:
lines
Out[154]:
['Tata,tata,500,50\n', 'Tata Infotech,tatainf,600,60']
In [160]:
lines[0].strip().split(",")
Out[160]:
['Tata', 'tata', '500', '50']
In [161]:
lines[0].strip().split(",")[0]
Out[161]:
'Tata'
In [162]:
lines[0].strip().split(",")[2]
Out[162]:
'500'
In [145]:
lines
Out[145]:
['Tata,tata,500,50\n']
In [157]:
stocks = readcsv_("stocks.csv")
In [158]:
stocks
Out[158]:
[['name', 'ticker', 'value', 'volume'],
 ['Infosys', 'infy', '1000', '500'],
 ['Tata', 'tata', '500', '50'],
 ['Reliance', 'reliance', '700', '100'],
 ['Tata Infotech', 'tatainf', '600', '60']]
In [165]:
def get_value(ticker, data):
    for row in data:
        if row[1] == ticker:
            return float(row[2])
In [166]:
get_value("infy", stocks)
Out[166]:
1000.0
In [167]:
def get_volume(ticker, data):
    for row in data:
        if row[1] == ticker:
            return float(row[3])
In [168]:
get_volume("infy", stocks)
Out[168]:
500.0

regular expression

In [169]:
import re
In [171]:
pattern = re.compile(r"^A.")
In [180]:
%%file regex1.txt
kjsahdhas 
kjsahdkjsa dfh
Asdf
Asjkadhas
Ab
jkshd
skajdh
A
Overwriting regex1.txt
In [181]:
with open("regex1.txt") as f:
    for line in f:
        if pattern.match(line.strip()):
            print(line, end="")
Asdf
Asjkadhas
Ab
In [182]:
def grep(pattern, filename):
    p = re.compile(pattern)
    with open(filename) as f:
        for line in f:
            if p.match(line.strip()):
                print(line, end="")
In [183]:
grep("^A.", "regex1.txt")
Asdf
Asjkadhas
Ab
In [187]:
grep("^A.$", "regex1.txt") # extactly one char
Ab
In [188]:
grep("^A.{1,3}$", "regex1.txt") # either 1, 2 or 2 chars
Asdf
Ab
In [189]:
grep("^A.+$", "regex1.txt") # atleast one char
Asdf
Asjkadhas
Ab
In [190]:
grep("^A{1,3}.+$", "regex1.txt") # A can come 1, 2 or 3 times, and rest one or more (any)
Asdf
Asjkadhas
Ab
In [192]:
grep("^A{2,3}.+", "regex1.txt") # A can come 2 or 3 times, and rest one or more (any)
In [194]:
grep("^A{1,3}.*", "regex1.txt") # A can come 1, 2 or 3 times, and rest zero or more (any)
Asdf
Asjkadhas
Ab
A
In [ ]: