def sum_shifted_rows(row):
first = row + [0]
second = [0] + row
return [a+b for a,b in zip(first, second)]
def pascal(n):
tr = [[1]]
for i in range(n-1):
tr.append(sum_shifted_rows(tr[-1]))
return tr
def prety_print(triangle):
def format_row(row):
return " ".join([str(item).rjust(boxsize) for item in row])
maxnum = max(triangle[-1])
boxsize = len(str(maxnum))
basesize = len(triangle[-1])
width = basesize*boxsize + basesize - 1
for row in triangle:
row_ = format_row(row)
print(row_.center(width))
prety_print(pascal(14))
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
import this
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 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!
Overwriting poem.txt
with open("poem.txt") as file:
print(file.read())
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 file:
for line in file:
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!
with open("poem.txt") as file:
for line in file:
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!
print("3") # it ends with \n
print(4)
3 4
print(3, end="")
print(4, end="")
34
file = open("poem.txt")
file.readline()
'The Zen of Python, by Tim Peters\n'
file.readline()
'\n'
file.readline()
'Beautiful is better than ugly.\n'
file.read()
"Explicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!\n"
file.read()
''
file.readline()
''
file.close()
with open("poem.txt") as f:
line = f.readline()
while line:
print(line, end="")
line = f.readline()
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!
if '':
print("false")
else:
print("true")
true
problems
reversed_line to print every line in reversed fashioncat.py which mimics unix comand cathead.py which mimics unix command head. it shows first n lines of a filetail.py which mimics unix command tail. it shows last n lines.def print_linenums(filename):
with open(filename) as f:
for linenum, line in enumerate(f, start=1):
print(linenum, line, end="")
print_linenums("poem.txt")
1 The Zen of Python, by Tim Peters 2 3 Beautiful is better than ugly. 4 Explicit is better than implicit. 5 Simple is better than complex. 6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases aren't special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless you're Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, it's a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- let's do more of those!
def print_reversed_lines(filename):
with open(filename) as f:
for line in f:
print(line.strip()[::-1])
print_reversed_lines("poem.txt")
sreteP miT yb ,nohtyP fo neZ ehT .ylgu naht retteb si lufituaeB .ticilpmi naht retteb si ticilpxE .xelpmoc naht retteb si elpmiS .detacilpmoc naht retteb si xelpmoC .detsen naht retteb si talF .esned naht retteb si esrapS .stnuoc ytilibadaeR .selur eht kaerb ot hguone laiceps t'nera sesac laicepS .ytirup staeb ytilacitcarp hguohtlA .yltnelis ssap reven dluohs srorrE .decnelis ylticilpxe sselnU .sseug ot noitatpmet eht esufer ,ytiugibma fo ecaf eht nI .ti od ot yaw suoivbo-- eno ylno ylbareferp dna --eno eb dluohs erehT .hctuD er'uoy sselnu tsrif ta suoivbo eb ton yam yaw taht hguohtlA .reven naht retteb si woN .won *thgir* naht retteb netfo si reven hguohtlA .aedi dab a s'ti ,nialpxe ot drah si noitatnemelpmi eht fI .aedi doog a eb yam ti ,nialpxe ot ysae si noitatnemelpmi eht fI !esoht fo erom od s'tel -- aedi taerg gniknoh eno era secapsemaN
%%file cat.py
import sys
def cat(filename):
with open(filename) as f:
print(f.read())
if __name__ == "__main__":
cat(sys.argv[1])
Writing cat.py
!python cat.py 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!
%%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__":
head(sys.argv[2], int(sys.argv[1]))
Writing head.py
!python 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.
%%file tail.py
import sys
def tail(filename, n):
with open(filename) as f:
window = []
for i in range(n):
window.append(f.readline())
line = f.readline()
while line:
window.pop(0)
window.append(line)
line = f.readline()
for l in window:
print(l, end="")
if __name__ == "__main__":
tail(sys.argv[2], int(sys.argv[1]))
Writing tail.py
!python tail.py 3 poem.txt
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!
homework problem
wc.py which prints three things, line count, word count, character count and filename
!python wc.py poem.txt
21 144 857 poem.txt
!wc poem.txt
21 144 857 poem.txt
with open("numbers.txt", "w") as f:
f.write("one\n")
f.write("two\n")
f.write("three\n")
!python cat.py numbers.txt
one two three
with open("numbers.bin", "wb") as f:
f.write(bytes([1, 2, 3, 4]))
!file numbers.bin
numbers.bin: data
!file numbers.bin
numbers.bin: ASCII text, with no line terminators
!cat numbers.bin
x0bzx45bx12f