In [176]:
import math
help(math.sqrt)
In [177]:
'c' in 'abc'
Out[177]:
In [178]:
s = "learn to program"
In [179]:
s[0:]
Out[179]:
In [180]:
s[-3:]
Out[180]:
In [181]:
s = s[:5] + 'ed' + s[5:]
In [182]:
wr = "Hi there dUde"
In [183]:
wr.lower()
Out[183]:
In [184]:
dir(wr)
Out[184]:
In [185]:
help(str.rfind)
In [186]:
help(str.strip)
In [187]:
wr.capitalize()
Out[187]:
In [188]:
def letter_by_letter(wr):
for char in wr:
print char
print char
In [189]:
letter_by_letter('abc')
In [190]:
def count_vowels(string):
running_sum = 0
for char in string.lower():
if char in 'aeiou':
running_sum = running_sum + 1
return running_sum
In [191]:
count_vowels('abA')
Out[191]:
In [192]:
grades = [50, 60, 75]
In [193]:
grades[0] # this is a value
Out[193]:
In [194]:
grades[0:1] # this is a list
Out[194]:
In [195]:
max(grades)
Out[195]:
In [203]:
students = ['Bob', 'Harry', 'Gilford']
In [204]:
max(students)
Out[204]:
In [202]:
float(sum(grades)) / float(len(grades))
Out[202]:
In [200]:
grades.append(75)
In [211]:
def wow(mylist):
collect = ''
for string in mylist:
collect = collect + ' ' + string
return collect.lstrip()
In [212]:
wow(students)
Out[212]:
In [216]:
students.sort()
wow(students)
Out[216]:
In [31]:
class Wallet:
def __init__(self, owner):
self.cash = 0
self.owner = owner
def addCash(self, amount):
self.cash = self.cash + amount
def printWallet(self):
print "Wallet belongs to {}, and has {}$".format(self.owner, str(self.cash))
In [32]:
w = Wallet("Bob")
w.printWallet()
In [33]:
w.addCash(10)
w.printWallet()
In [34]:
help(range)
In [36]:
range(1,
Out[36]:
In [27]:
help(repr)
In [28]:
repr(w)
Out[28]:
In [29]:
repr(3)
Out[29]:
In [30]:
repr('3')
Out[30]:
In [39]:
import json
In [40]:
json.dumps(['a'])
Out[40]:
In [1]:
print "I'm the worst developer {}".format("ever")
In [1]:
import urllib
In [2]:
f = urllib.urlopen("http://www.gutenberg.org/ebooks/3420.txt.utf-8")
In [3]:
book_text = f.read().decode('utf-8')
In [4]:
book_text = book_text.replace("\r","")
In [12]:
print book_text[14000:15000]
In [8]:
import re
In [10]:
re.UNICODE = True
re.DOTALL = True
In [13]:
book_text = re.split("\*END THE SMALL PRINT! FOR PUBLIC DOMAIN ETEXTS\*.*", book_text)[1]
In [14]:
len(book_text)
Out[14]:
In [23]:
print book_text[:500]
print "[...]"
print book_text[-150:]
In [18]:
book_text = book_text.strip()
In [22]:
help(re.sub)
In [24]:
book_text = re.sub("This etext was produced by(?:\n.+)*\n*?", "", book_text)
In [26]:
book_text = re.sub("\n.*$", "", book_text)
In [27]:
book_text = book_text.strip()
In [29]:
print book_text[:100]
print "[...]"
print book_text[-100:]
In [31]:
book_text = book_text.lower()
book_text = re.sub("[^\w\s]", "", book_text)
In [32]:
words = re.split("[\s]+", book_text)
In [33]:
len(words)
Out[33]:
In [34]:
f = urllib.urlopen("http://louis.philotech.org/confs/MtlPythonEn2014/corpus/stoplist.en.utf-8.txt")
stoplist = f.read().decode('utf-8').split("\n")
In [35]:
len(stoplist)
Out[35]:
In [36]:
stoplist[:50]
Out[36]:
In [37]:
words = [ w for w in words if len(w) > 1 and w.isalpha() ]
words = [ w for w in words if w not in stoplist ]
len(words)
Out[37]:
In [38]:
help(xrange)
In [39]:
t = 50
words_seg = [ words[i:i+t] for i in xrange(0, len(words), t) ]
In [40]:
len(words_seg)
Out[40]:
In [41]:
print words_seg[0]
In [ ]: