import requests
def getprice(pid):
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'
url = 'https://www.amazon.com/dp/'+pid
s = requests.get(url, headers={'User-Agent':ua})
pattern = '<span id="priceblock_ourprice" class="a-size-medium a-color-price">$'
price = float( s.text.split(pattern)[-1].split('</span>')[0] )
return price
getprice('B0027YPQEC')
#'https://www.amazon.com/dp/B0027YPQEC'
s ='abcdefghijklmno'
s[3]
s[1:5]
s[::2]
s[::-1]
words = open('class2_files/words.txt').read().split('\n')
words[:10]
sorted(words)[:10]
sorted(words,reverse=True)[:10]
otherwords = ['axe','cable','apple']
sorted(otherwords)
def second(x): return x[1]
second('abcdf')
#print(otherwords)
#print(otherwords(otherwords[0]))
#print(otherwords(otherwords[1]))
#print(otherwords(otherwords[2]))
sorted(otherwords,key=second)
def righttoleft(x): return x[::-1]
#righttoleft('abcdef')
print(righttoleft(otherwords[0]))
print(righttoleft(otherwords[1]))
print(righttoleft(otherwords[2]))
sorted(otherwords,key=righttoleft)
#sorted(words,key=righttoleft)
for word in otherwords:
print( '{:>85}'.format(word))
print(otherwords)
start = otherwords.index('apple')
print(start)
#otherwords[start:start+2]
def righttoleft(x): return x[::-1]
reorderedwords = sorted( words, key=righttoleft)
#print(reorderedwords)
start = reorderedwords.index('chuck')
reorderedwords[start]
start = reorderedwords.index('bit')
print(start)
for word in reorderedwords[start:start+20]:
print('{:>25}'.format(word))
def ispal(w):
return w==w[::-1]
#s = 'loop'
#'pool' == s[::-1]
for word in words:
if ispal(word) and len(word)>1:
print(word)
print('a'*5)
w=[]
for l in 'abcd':
if l!='a':
w.append(l*5)
print(w)
w2 = [l*5 for l in 'abcd' if l!='a']
print(w2)
w1 = []
for word in words:
if len(word)>0 and not word[0].isupper(): # issupper() means the string contains only UPPER CASED and at least 1
w1.append(word)
#print(w1)
#w = [word for word in words if len(word)>0 and not word[0].isupper()]
#print(w)
len(words),len(w)
w = [word for word in words if len(word)>0 and not word[0].isupper() and not word.endswith("'s")]
len(words),len(w)
#print(w)
swords = set(words)
reversiblewords1 = []
for word in words:
if word[::-1] in swords and len(word)>1:
reversiblewords1.append((word,word[::-1]))
reversiblewords1[:10]
#reversiblewords = [(word,word[::-1]) for word in words if word[::-1] in swords and len(word)>1]
#for b,d in reversiblewords:
# if b<=d and len(b)>4: print( '{:>25} {:<25}'.format(b,d) )
from time import time
longlist = list(range(0,40000000))
tic = time()
-1 in longlist
toc = time()
toc-tic
slonglist = set(longlist)
tic = time()
-1 in slonglist
toc = time()
toc-tic
Testing for membership in the set is 10,000 times faster in this example!
d = {'apple':'crunchy fruit','banana':'long yellow fruit','zebra':'striped animal'}
d['zebra']
d.keys()
for item in d:
print(item)
'banana' in d
d['duck'] = 'quacking bird'
d
s = 'Hello, there!'
s.replace(',','')
punc = ',.;!?'
for p in punc: s = s.replace(p,'')
s
for x in punc:
print(x)