Artificial Intelligence with Python
By Charlie Temple
BOOK 1
PYTHON ADVANCED
HOW
TO
MASTER PYTHON
INTRODUCTION
PYTHN 3.0 (a.k.a. "Pthn 3000" r "P3k") i a nw rin f th language tht is incompatible with the 2.x lin f releases. Ovr tim, Pthn h xldd in popularity, frm bing an bur scripting lngug t becoming n of th most ulr, and widl ud lngug in th wrld. In thi ur, Advnd Pthn, Python 3 is th most u-t-dt version f the language with mn improvements made t increase th ffiin nd simplicity f the d that u writ. Yull learn dvnd topics, a knwldg f whih will t you rt frm th grtr numbr of Python dvlr. Firt, u'll xlr hw t gin fin-grind ntrl over ttribut . Next, u'll divr hw t intercept l-bjt construction. Finll, u'll learn th ubtl, but wrful ntrl Pthn giv u over l rltinhi. B the end f this ur, u'll knw nugh Python t undrtnd th advanced thn i u used t imlmnt hititd frameworks nd muh more.
Bming a Python xrt tk tim, but vr tim ull mtr thi butiful rgrmming lngug. It wrth it!
Cright 2020. All right reserved.
Thi dumnt i grd twrd rviding xt nd rlibl infrmtin in rgrd t th ti nd iu vrd. Th ublitin i ld n th id tht th ublihr i nt ruird t rndr unting, ffiill rmittd, r thrwi, ulifid rvi. If dvi i nr, lgl r rfinl, a rtid individul in th rfin huld b rdrd.
Frm a Dlrtin f Prinil whih w accepted nd rvd ull b a Cmmitt f th Amrin Br Aitin nd a Cmmitt f Publihr nd Aitin.
In n w, it is lgl t rrdu, dulit, r trnmit n rt f thi dumnt b ithr ltrni mn r in rintd frmt. Rrding f thi ublitin i tritl rhibitd nd n trg f thi dumnt i nt llwd unl with writtn rmiin frm th ublihr.
All right rrvd.
Th infrmtin rvidd hrin i ttd t b truthful nd nitnt, in tht n libilit, in trm f inttntin r thrwi, b n ug r bu f n lii, r, r dirtin ntind within i th litr nd uttr rnibilit f th riint rdr.
Undr n irumtn will any lgl rnibilit r blm b hld gint th ublihr fr n rrtin, r mntr l du t th infrmtin hrin, ithr dirtl r indirtl.
Rtiv uthr wn ll right nt hld b th ublihr.
Th infrmtin hrin i ffrd fr infrmtinl ur ll nd i univrl . Th rnttin f th infrmtin i withut a ntrt r n t of gurnt urn.
Th trdmrk tht r ud r withut n nnt, nd th ublitin f th trdmrk i withut permission r bking b th trdmrk wnr. All trdmrk nd brnd within thi bk r fr clarifying ur nl nd r wnd b th wnr thmlv, nt ffilitd with thi dumnt.
TABLE OF CONTENT
BOOK - I
BOOK - II
PYTHON 3.0, THE GRAND MASTER
WHT NW IN PYTHON 3.0
Th uming end-of-support ddlin for Pthn 2 n Jnur 1, 2020 mn enterprises nd to trt thinking but th imt it will hv n their existing Pthn litin.
Originll, Pthn 2 w ud as a scripting ltrntiv t Perl fr thing like nfigurtin automation or else for rting wb applications. But Pthn dtin didnt rll tk ff until its dt in kg trtd bming ulr m 5-6 r g.
Pthn 2 vs 3
Dit th ft tht Python 3 w originally intrdud in 2008 and w well established by th tim dt in rjt trtd lrting the ulrit of Pthn, dt scientists, by nd lrg, were still wrking with Python 2.
In fact, Pthn 2 i rving t b a rtiulrl diffiult hbit to kick:
Many ulr rting tm (like M OS) continue to inrrt Pthn 2 th default intlltin or rvid bth Pthn 2 and 3 ut f th box (Dbin, SUSE, etc.).
A rntl as Augut 2016, 90% of ll i installs were fr Pthn 2.
It wnt until Jun 2017 that Python 3 ur trtd utnumbring Python 2 ur, rding to PChrm.
Th good news is tht Pthn 3 i (finll) starting t gain ground:
94% f the top 360 mt dwnldd packages ffr Pthn 3 urt.
Th thr lrgt ubli lud providers (AWS, Azur, and Ggl
Cloud Pltfrm) full support Python 3.
Python 3.5 intrdud hng tht imlifid code rting.
As a rult, if youre rting new rjt td, you should b uing Pthn 3 unl theres a specific kg u nd that till dnt urt 3.
COMMON IMPROVEMENT IN PYTHON 3.0
Print Is A Function
Th print ttmnt h bn rld with a print() function, with keyword rgumnt to rl mt f the il ntx f th ld rint statement ( PEP 3105). Exml:
Old: rint "The nwr i", 2*2
Nw: rint("Th nwr i", 2*2)
Old: rint x, # Trailing mm suppresses nwlin New: rint(x, nd=" ") # And a intd f a nwlin
Old: print # Prints a nwlin
New: print() # You mut ll th funtin!
Old: rint >>sys.stderr, "ftl rrr"
Nw: print("fatal rrr", fil=.tdrr)
Old: rint (x, ) # rint rr((x, y))
New: rint((x, )) # Not th same as print(x, )!
Yu n l customize th separator btwn itm, .g.:
print("There are <", 2**32, "> ibiliti!", ="")
which rdu:
Thr r <4294967296> ibiliti!
Nt:
Th print() function dnt urt th ft feature f the ld
rint ttmnt. For xml, in Python 2.x, rint "A\n", "B" wuld writ "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n".
Initill, ull b finding urlf ting th ld rint x a lt in intrtiv mode. Tim t rtrin ur fingers to type print(x) intd!
Whn uing th 2t3 source-to-source conversion tool, ll rint ttmnt r automatically nvrtd t rint() function calls, so thi i mostly a non-issue for larger rjt.
Viw And Itrtr Instead Of Lit Sm wll-knwn API n longer return lit: dit mthd dit.k(), dit.itm() and dict.values() return views intd f lists. Fr example, this n lngr wrk: k = d.k(); k.rt(). Use k = rtd(d) intd (thi wrk in Python 2.5 t nd is just as ffiint).
Al, th dit.itrk(), dit.itritm() nd dit.itrvlu() mthd r no lngr urtd.
M() nd filtr() return itrtr. If you really nd a lit, a quick fix i e.g. list(map(...)), but a bttr fix i often t u a list mrhnin (ill whn th original d uses lmbd), r rwriting the code so it dnt nd a lit at ll. Particularly trik i m() invkd fr th id effects of th funtin; the correct transformation is to use a regular fr l (in rting a list wuld just b wtful).
rng() nw bhv lik xrange() ud t behave, except it wrk with values f rbitrr iz. The lttr n longer exists.
zip() now rturn n itrtr.
Ordering Comparisons
Python 3.0 h imlifid th rul fr rdring mrin:
The ordering mrin operators (<, <=, >=, >) raise a Type
Error xtin whn th operands dont have a meaningful
nturl rdring. Thus, xrin like 1 < '', 0 > Nn or ln <= len r no lngr vlid, nd .g. Nn < None ri TypeError instead f rturning Fl. A rllr i tht rting a htrgnu lit n longer mk n all th elements mut b mrbl t each thr. Nt that this does nt apply to the == nd != rtr: bjt f different inmrbl t lw compare unul t h thr.
builtin.rtd() nd lit.rt() no lngr t th m argument providing a mrin function. U th key rgumnt instead.
N.B. th k nd rvr rgumnt r nw keyword-only.
The cmp() funtin huld be trtd gn, nd th __m__() special mthd is n lngr urtd. Use __lt__() fr rting,____() with __hh__(), nd other rih mrin ndd.
(If u rll need the m() funtinlit, u uld use th expression ( > b) - ( < b) th equivalent fr cmp(a, b).)
Intgr
PEP 0237: Essentially, lng renamed to int. That is, there i nl one built-in intgrl type, nmd int; but it behaves mostly lik th ld lng t.
PEP 0238: An xrin like 1/2 rturn a flt. U 1//2 t gt th trunting bhvir. (The latter syntax h xitd fr r, at lt in Pthn 2.2.)
Th .mxint ntnt was removed, since thr is no lngr a limit t th value f intgr. Hwvr, .mxiz can be used n intgr lrgr thn n rtil lit or tring index. It conforms t the imlmnttin natural integer size nd is typically the same .mxint in rviu releases on th same ltfrm (assuming th m build tin).
Th rr() of a long integer dnt inlud th trailing L nmr, code that unnditinll tri tht hrtr will h off the lt digit instead. (Use tr() instead.)
Octal literals r n lngr f th form 0720; u 0720 intd.