Keith Maull
Jan. 03, 2019
a_simple_list = ['a', 'b', 'c', 'd']
if a_simple_list[0] == 'a':
print("The first item of the list is an `a`")
The first item of the list is an `a`
for item in a_simple_list:
print(item)
a b c d
count = 0
while (count < 5):
print(count)
count+=1
0 1 2 3 4
# functions are easy
def my_func(x, y):
return x*y
my_func(3,4)
12
# random numbers generator
import random
print(random.randint(0,100))
89
# http library
import requests
r = requests.get("https://raw.githubusercontent.com/atebits/Words/master/Words/en.txt")
if r.status_code == 200:
data = r.text
print(data[:1000])
aa aah aahed aahing aahs aal aalii aaliis aals aardvark aardvarks aardwolf aardwolves aargh aarrgh aarrghh aarti aartis aas aasvogel aasvogels ab aba abac abaca abacas abaci aback abacs abacterial abactinal abactinally abactor abactors abacus abacuses abaft abaka abakas abalone abalones abamp abampere abamperes abamps aband abanded abanding abandon abandoned abandonedly abandonee abandonees abandoner abandoners abandoning abandonment abandonments abandons abandonware abandonwares abands abapical abas abase abased abasedly abasement abasements abaser abasers abases abash abashed abashedly abashes abashing abashless abashment abashments abasia abasias abasing abask abatable abate abated abatement abatements abater abaters abates abating abatis abatises abator abators abattis abattises abattoir abattoirs abattu abature abatures abaxial abaxile abaya abayas abb abba abbacies abbacy abbas abbatial abbe abbed abbes abbess abbesses abbey abbeys abbot abbotcies abbotcy abbots abbotship abbotsh
data.split('\n')[:10]
['aa', 'aah', 'aahed', 'aahing', 'aahs', 'aal', 'aalii', 'aaliis', 'aals', 'aardvark']
top_100_five_letter_words = []
for w in data.split('\n'):
if len(w) == 5:
top_100_five_letter_words.append(w)
if len(top_100_five_letter_words) == 100:
break
print(top_100_five_letter_words)
['aahed', 'aalii', 'aargh', 'aarti', 'abaca', 'abaci', 'aback', 'abacs', 'abaft', 'abaka', 'abamp', 'aband', 'abase', 'abash', 'abask', 'abate', 'abaya', 'abbas', 'abbed', 'abbes', 'abbey', 'abbot', 'abcee', 'abeam', 'abear', 'abele', 'abets', 'abhor', 'abide', 'abies', 'abled', 'abler', 'ables', 'ablet', 'ablow', 'abmho', 'abode', 'abohm', 'aboil', 'aboma', 'aboon', 'abord', 'abore', 'abort', 'about', 'above', 'abram', 'abray', 'abrim', 'abrin', 'abris', 'absey', 'absit', 'abuna', 'abune', 'abuse', 'abuts', 'abuzz', 'abyes', 'abysm', 'abyss', 'acais', 'acari', 'accas', 'accoy', 'acerb', 'acers', 'aceta', 'achar', 'ached', 'aches', 'achoo', 'acids', 'acidy', 'acing', 'acini', 'ackee', 'acker', 'acmes', 'acmic', 'acned', 'acnes', 'acock', 'acold', 'acorn', 'acred', 'acres', 'acrid', 'acted', 'actin', 'acton', 'actor', 'acute', 'acyls', 'adage', 'adapt', 'adaws', 'adays', 'addax', 'added']
top_100_five_letter_words_ending_in_g = []
for w in data.split('\n'):
if len(w) == 5 and w[-1] is 'g':
top_100_five_letter_words_ending_in_g.append(w)
if len(top_100_five_letter_words_ending_in_g) == 100:
break
print(top_100_five_letter_words_ending_in_g)
['acing', 'aging', 'ahing', 'aking', 'alang', 'almug', 'along', 'among', 'aping', 'awing', 'axing', 'befog', 'being', 'bewig', 'bhang', 'bling', 'boing', 'boong', 'bourg', 'bring', 'brung', 'chang', 'clang', 'cling', 'clung', 'cohog', 'colog', 'craig', 'cuing', 'debag', 'debug', 'defog', 'derig', 'doing', 'droog', 'duing', 'dwang', 'dying', 'ehing', 'eking', 'embog', 'emong', 'ennog', 'ering', 'exing', 'eying', 'fling', 'flong', 'flung', 'glogg', 'going', 'gulag', 'hoing', 'hying', 'hyleg', 'icing', 'incog', 'iring', 'kaing', 'kiang', 'klang', 'klieg', 'klong', 'krang', 'kreng', 'kyang', 'liang', 'lolog', 'lying', 'moong', 'nying', 'obang', 'oflag', 'ohing', 'oping', 'orang', 'owing', 'phang', 'piing', 'pirog', 'pling', 'plong', 'prang', 'prong', 'rejig', 'renig', 'repeg', 'rerig', 'retag', 'rolag', 'ruing', 'scoog', 'scoug', 'scrag', 'scrog', 'shrug', 'skegg', 'slang', 'sling', 'slung']
import json
import requests
r = requests.get("https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json")
if r.status_code == 200:
pet_data = json.loads(r.text)
print(data)
IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable `--NotebookApp.iopub_data_rate_limit`. Current values: NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) NotebookApp.rate_limit_window=3.0 (secs)
pet_data['pets'][0]
{'name': 'Purrsloud', 'species': 'Cat', 'favFoods': ['wet food', 'dry food', '<strong>any</strong> food'], 'birthYear': 2016, 'photo': 'https://learnwebcode.github.io/json-example/images/cat-2.jpg'}
# regular expressions
import re
s = r'^t.*ers$' # all words starting with `t` ending in `ers`
for w in data.split('\n'):
if re.match(s, w):
print(w)
tabasheers tabers tabliers taborers tabourers tacheometers tachometers tachygraphers tachymeters tackers tackifiers tacklers taggers tailenders tailers tailgaters tailwaters taivers takeovers takers talebearers talers talkers talliers tambers tambourers tamers tamperers tampers tanagers tanglers tankbusters tankers tanners tantalisers tantalizers taperers tapers tappers tapsters targeteers tarnishers tarpapers tarriers tarsiers taseometers tasers tasimeters taskers taskmasters tastemakers tasters taters tatlers tatters tattlers tattooers taunters tautomers taverners tavers tawers taxameters taxers taximeters taxonomers taxpayers teachers teamakers teamers teamsters tearers tearjerkers teaselers teasellers teasers teatasters tedders teemers teenagers teeners teenyboppers teers teeters teethers teetotalers teetotallers telecasters telecommuters teleconverters telegraphers telemarketers telemeters telephoners teleprinters teletypewriters televiewers televisers teleworkers telewriters telfers tellers tellurometers telphers temperers tempers temporisers temporizers tempters tenderers tenderisers tenderizers tenderometers tenders tenners tenoners tenpounders tensimeters tensiometers tensioners tenters tentmakers termers terminers terpolymers terriers terrifiers terrorisers terrorizers testers testifiers tethers tetramers tetrameters tetters teuchters texters thalers thankers thanksgivers thatchers thawers theatergoers theaters theologasters theologers theologisers theologizers theorisers theorizers theosophers thermographers thermometers thickeners thiggers thillers thimbleriggers thinkers thinners thirsters thrashers threaders threadmakers threapers threateners threepers threshers thrillers thrivers throbbers thronners throttlers throwers throwsters thrummers thrusters thumpers thunderers thunders thundershowers thurifers thwackers thwarters tickers ticklers tiddlers tidewaiters tidewaters tidiers tiebreakers tiers tigers tighteners tilers tillers tilters tiltmeters timbers timekeepers timepleasers timers timesavers timeservers timeworkers timoneers tinders tinglers tinkerers tinkers tinklers tinners tinters tintometers tippers tipplers tipsters titers titfers tithers titleholders titlers titterers titters toadeaters toasters toastmasters tobogganers tochers toddlers todgers toeraggers toggers togglers toilers tokers tollers tolters toners tongers tongsters tonguesters tonkers tonners tonometers tontiners toolers toolholders toolmakers toolpushers tooters tootlers topers topliners topmakers topnotchers topographers toppers topsiders torchbearers torchers torchiers tormenters torpedoers torquers torturers toshers tossers totalisers totalizers toters totterers totters touchers touchpapers tougheners tourers tourneyers tousers touters towers towsers toyers tracers trackers tracklayers trackwalkers traders traditioners traducers traffickers trailblazers trailbreakers trailers trainbearers trainers trammelers trammellers trampers tramplers trampoliners tranquilisers tranquilizers tranquillisers tranquillizers transceivers transcribers transducers transferrers transfers transformers transfusers transgenders transhippers transmissometers transmitters transmuters transplanters transponders transporters transposers transputers transshippers transvaluers transverters tranters trapanners trappers trapshooters trashers travelers travellers traversers trawlers treacherers treachers treaders treadlers treasurers treaters treehoppers trekkers tremblers trenchers trendsetters trepanners trephiners trespassers triaconters tribometers tributers trickers tricksters tricyclers triers triflers triggers trigonometers trillers trimers trimesters trimeters trimmers trinketers triphammers trippers tripplers triumphers trochanters trocheameters trochometers troffers trollers tromometers troopers troposcatters trossers trotters troublemakers troublers troubleshooters trouncers troupers trousers trouters trovers trowelers trowellers trowsers truckers trucklers truckmasters trudgers trumpeters truncheoners trundlers trussers trustbusters trusters tryers trysters tubbers tubers tuckers tufters tuggers tumblers tummlers tuners tunnelers tunnellers turbidimeters turbochargers turcopoliers turners turnovers turtlers tushkers tuskers tussers tutoyers tutworkers tuyers twaddlers twangers twanglers twattlers tweakers tweedlers tweenagers tweeners tweers tweeters tweezers twicers twiddlers twiers twiggers twiners twinflowers twinklers twinters twirlers twisters twitchers twitterers twitters twoccers twockers twoers twofers twoseaters twyers tyers tylers typecasters typefounders typesetters typewriters typifiers typographers tyrannisers tyrannizers
1
, 1.87
, -0.88
, ...)"Hello"
, 'Hello'
)True
, False
)None
### No surprises here ...
1 + 2
3
'hello' + "Hello"
'helloHello'
lst = [1, 2, "three"]
print(lst[1])
2
for i in lst:
print(i, end=" ")
1 2 three
lst + ['a', 'b']
[1, 2, 'three', 'a', 'b']
enumerate
for idx, l in enumerate(lst):
print("{}:{}".format(idx, l), end=" ")
0:1 1:2 2:three
lst[-1]
'three'
lst[0:2]
[1, 2]
lst[::-1]
['three', 2, 1]
(1, 2, 'three
)set([1,1,1,1,1,1,2,3,4,5,5,5,5,5,5,5])
{1, 2, 3, 4, 5}
setA = set([1,2])
setB = set([1,2,3,4,5])
setA.union(setB)
{1, 2, 3, 4, 5}
setA.difference(setB)
set()
setB.difference(setA)
{3, 4, 5}
setA.intersection(setB)
{1, 2}
d = {"a": 5, "b": 6}
print(d)
{'a': 5, 'b': 6}
d = dict([('a', 5), ('b', 6)])
print(d)
{'a': 5, 'b': 6}
d['a']
5
d.keys()
dict_keys(['a', 'b'])
d.values()
dict_values([5, 6])
for key, value in d.items():
print("{}=>{}".format(key, value))
a=>5 b=>6
in
and is
not
, not in
, is not
, and
, or
1 is 2
False
1 is not 2
True
True and True
True
True or False
True
False or False
False
False is not True
True
s = 'supercalifragilistic'
'u' in s
True
'z' not in s
True
lst = ['a', 'b', 1, 2, 3]
'a' in lst
True
s == 'supercalifragilistic'
True
s is 'supercalifragilistic'
True
lst == ['a', 'b', 1, 2, 3]
True
lst is ['a', 'b', 1, 2, 3]
False
if/elif/else
for
while
s = 'this is a test'
if s[0] == 't':
print("the first letter is a 't'")
else:
print("the first letter is not a 't'")
the first letter is a 't'
for c in s:
print("{}".format(c))
t h i s i s a t e s t
# THIS IS NOT PYTHONIC! for is more elegant
i = 0
while(i < len(s)):
print("{}".format(s[i]))
i+=1
t h i s i s a t e s t
min
, max
, all
, any
zip
, range
, len
, map
, sorted
reversed
chr()
built in functionHINT 1:
chr(122)
'z'
import random
for i in range(0, 17):
# get a number in the ASCII range
while(True):
rnd_c = random.randint(48, 123)
if rnd_c < 58 or (rnd_c > 65 and rnd_c <91) or (rnd_c > 96 and rnd_c < 123):
break
print(chr(rnd_c), end="")
uMVKgvYyB114e4rpj
def a_function():
return 1
a_function()
1
Note: default values are allowed!
def a_function(a, b, c):
return a*b*c
a_function(1,2,3)
6
def a_function(a=1, b=1, c=1):
return a*b*c
a_function(a=3,b=8,c=8)
192
a_function(a=4)
4
a_function(3,b=4,c=4)
48
def a_function(a, b=1, c=1):
return a*b*c
a_function(6,b=8,c=8)
384
a_function(6, 8, 8)
384
def b_function():
return (1, 2, 3)
b_function()
(1, 2, 3)
x, y, z = b_function()
print(x, y, z)
1 2 3
try
/except
/finally
and they should be used regularlydef c_function(a, b, c):
try:
if a<0:
raise Exception
return a*b*c
except Exception as e:
print("The first parameter cannot be less than zero.")
finally:
pass # we don't need to clean up anything after the exception
c_function(-1, 2, 3)
The first parameter cannot be less than zero.
ALL PYTHON FUNCTIONS IMPLICITLY RETURN None
WHEN NO RETURN VALUE IS SPECIFIED
c_function(-1, 2, 3) is None
The first parameter cannot be less than zero.
True
l1 = [x for x in range(0,11)]
l1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def pw_generator():
ascii_pw_range = list(range(48,58)) + list(range(65,91)) + list(range (97, 123))
return ''.join([chr(random.choice(ascii_pw_range)) \
for i in range(0,10)])
pw_generator()
'TMbD0xr0Bj'
[pw_generator() for i in range(0,15)]
['MIO3ewedYc', 'YbFp3ofMhC', 'lgy5GUvod4', 'VfOjbJ1oF3', 'tqYltK37KK', 'E4SzZ0zRjX', 'fdJRpvyHrn', '9cCas9FpyA', '33qhlSo7kd', 'dL1QupAp0V', 'KBciH7ZSaN', '3B1StGRLLe', 'bR6wZCAzsa', '9gq3zcB6lb', 'vmNodUhosK']
d1 = dict([('a', 1), ('b', 2), ('c', 3)])
{ v:k for (k,v) in d1.items() }
{1: 'a', 2: 'b', 3: 'c'}
d1 = dict([('a', 1), ('b', 2), ('c', 3)])
{ v:k for (k,v) in d1.items() if v%2 != 0}
{1: 'a', 3: 'c'}