We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 164
’ CODING
ACADEMY
@2023
LEARN TO CODE FAST TODAY!
* PYTHON + RUST » ERLANG
* MONGO « REDIS * GO + RIAK
164 PAGES OF EXPERT
GUIDES & TUTORIALS
LEARN CORE
CODING TECHNIQUES AND
ADVANCED SKILLSapa de RARRRAARAAR
I
hme Ds Ds MN li lin Df
is mi
ehh ShabhadhhRAh
: hin . a NN
mi ie |
a3 SSRSEREESESS
» BARRA pee
SESSEERERERESS
BERERESESEEERERE
SESSSeESRe a
eA a
ry rvPr|You holdin your handsa gateway tothe word of programming. nan era
‘when the onine word runs the world the people that can code that word,
rule it. Just take afew of the bggest online names inthe wold fom the
‘creator of Facebook, Mark Zuckerberg tothe quartet that created Twitter to
‘Google founders Lary Page and Sergey Brin ~ they are al programmes.
‘ut you dont need to aim to create the next bln pound startup tohavea
lus for earning to code. Coding can be fun and entertaining: itcan open the
{gates to creating amazing projects used online by entire communities, down
‘to small physical installations running at home ona Raspberry Pi the only
limit yourimagination. Coding Academy takes you through the
‘fundamentals flaring to code, offers a hefty coding reference atthe back,
and helps you lex your new programming muscles witha host of practical
Python projects, introducing the basics of database interactions and covering
the cutting-edge languages of Erang, Go, and Rust. Lets get coding!4 L
FUTURE
4 rCODING
ACADEMY
2023Contents
CODING
ACADEMY
Fundamentals Projects
24
26
28
6 | Coding Academy
Different types of data 32. Python 3: How to get started
How Python handles various ‘re moving to Python 3! About time
More Python data types 36 Python 3: Using functions
Beyond the previous ones, of course The essentials of modules, classes and more
‘modules done quickly 44
break things apart Automate your
scripts
with scripts:
Write your own UNIX program 48 Python: times, dates & numbers
Rewriting cat for fun and (no) profit Essential processing of complex dates
Neater code with modules 52 NumPy & SciPy: For science!
Use them wisely for super-clean progra
Embrace storage & persistence
Deal with persistent data in Python
Lock down with data encryption
Look after your data and it will look after you
“Coding can be fun and
entertaining; it can open
the gates to creating
amazing projects”Databases
58 Python and SQLite
Get started using databases
62. Get to grips with MariaDB.
‘The open source alternative to MySQL
Mik maximum power from Mongo
74 —MongoDB: Build a blog
Get your words collected in a database
78 Riak NoSQL.
Just what isthe big deal about NoSQL?
82 Redis data store
High-speed, in memory NoSQL
Reference
140. Get to grips with Python lists
Its one thing after another, isn'tit?
142. Understanding functions and objects
All about things and what things do
144 Adapt and evolve with conditional
Because change is inevitable
146. Variable scope of various variables
There's a variety of variables, and they vary
148. Recursion: round and round we go
Repetition is no bad thing
150 Super sorting algorithms
Get everything orderly, and qui
Contents
Do more
88
94
100
104
108
uz
u6
126
132
Program in Erlang: Introduction
Get to grips with the Eriang language
Erlang: Functions
Discover functions and basic data types
Rust: modules and cargo
Get started with the hip new language
Rust: functions and modules
Go further tacking error handling
Rust: file 1/0
Speed up your transfers
Rust: networking
Build a TCP-based client/server tool
Rust: concurrency
How to use Threads
Go: Master Google's new
programming language
Get yourself familiar with the fundamentals
Go: Data types
Get to grips with Go's composite data types
Go: Explore functions
Learn how you can develop and use functions
Hidden secrets of numbers
Integers thoroughly demystified
Using loops and using loops
And using loops and using loops
‘The magic of compilers
How they translate your code
Avoid common coding mistakes
Getit right the first time
Coding Academy | 7CODING
ACADEMYUsing Python, we'll take you
through essential coding steps
Different types of data
How Python handles various variables
More Python data types
Beyond the previous ones, of course
Reliability by abstraction
Open your coding mind and think diferently
Files and modules done quickly
‘The easy way to break things apart
Write your own UNIX program
Rewriting cat for fun and (no) profit
Neater code with modules
Use them wisely for super-clean programs
Embrace storage & persistenct
Deal with persistent data using Python
Lock down with data encryption
Look after your data and it will look after you
Coding Academy | 9Fundamentals
Different types of
Python data
Functions tell programs how to work, but it’s data that they operate
on. Let's go through the basics of data in Python
fundamental concepts of coding Here we look at basic
datatypes: nlater articles, wallock at a few
‘more advanced topics that bul on wht we do ere: data
abstraction fancy structures such as trees, and mere.
What is data?
Inthe word andin the programs that wel wrt, there's
an amazin varety of dierent types of data. na mortage
calculator. fr example, the value of the mortgage the
interest rate andthe term ofthe loan ar all types of data
ina shoping list program, there areal te ferent types
‘food andthe ist tat stores them ~ each of which has
its own kind of data,
‘The computer's words alt mor ite. Itdoesnit
know theeiffrence between althese datatypes. but that
oesnt stop rom working with them, The computer has
‘afew basic ones can werk wth, and that you have to use
creatively to represent athe varity n the world
‘ell begin by highighting thre data types: frst. we
I nthis section, we be using Python to broach many ofthe
Date wee Roemer 103 nd 580 ea rapes hese
tctigcnyy_Thoprtcr roberts oop’ thon knows
Stoesarts abate types clues ta nein ong Core
Sreninveal gu ats (bers wh cma pont sacha 1035
brows tng S913) onl comple crlexrmbe) These do
few Ss suchas Mllo Wort "Banana ona’ Tess
Sea are identified as a sequence of characters enclosed within
7 ‘quotation marks. You can use either double or single quotes.
ae case youll tt ts uch {Bananas Oranges)
‘accidentally caught by code that catches txcertion
propagate up and cause the interpreter to exit
(Changed in version 2.5: Changed to inherit from ease
Ralsed when an operation or function is applied to|
associated value is a string giving details about the t
‘exception UnboundLocalerror
Ralsed when a reference is made to a local variable
has been bound to that variable, This is a subclass of
New in version 2.0.
exception unicodeérror
Ralsed when a Unicode-related encoding or decoa|
10 | Coding Academy
In some ways these are tke astrng in tha they area
sequence, What makes them iferentis thatthe elements
that make up alist canbe of ny type
Inthis example, te elements areal strings. but ou could
create another list that mses cert types, such as
[PBanaras; 1,2] Lists are enti’ by the square
brackets that enclose them. andeach em or element within
them is separated by a comme,
Working with data
There seb of hing youcan do wth the ferent yes
dno Python For stoce yovean a sec Sl ord
mut wo nantes and rhon itn ees
were
&
pan
2
yaa corte ferent ype ofrumbers suchas anit
and aoa. teva eure Plone ol uhatver
te etn the mos eta thet toy youn
Sata. he reurnedvae bea fo
Yovcantest ths byusre the ped ncn tretars
theta pect watever anette pesto
> pa
“pert
sree)
‘ope oat
sepa +2300
‘ope oats
Moca lose he srr persons srg adits,
but they he tere fet Te epertr concatenate
thaiscombnestepter tw Snes ook whl the?
pert open thecontrs ofthe sonar
peo” +r”
ao Wo
>>topee)*2
Cals ae
Surreal aso have teow speci sett operat,
ising ses Tse este yout ek
Spartator par othe sence yt rumen
wich begs tom 0.
per Hela
=a
=
>> wor
ana cake tit)
> lata
‘iioIndexes workin verse too. I you want to reference the last
clement of alist or the ast characterin a string, you can use
the same notation with a Las the index. -2 wil eerence the
second to last character, 3th thd, and soon. Note that
‘when working backwards. theindexes don't star at
Methods
st and strings lsc have a range of ther special
operations, each unique to that particular type. These are
noun as methods. They similar to functions such as
‘typed in that they performa procedure What makes them
dferetis that they're associated witha particular piece
of data, and hence havea cterent syntax for execution
For example, among the ist type's methods ae append
andinsert,
>»vlist append chicken)
>>list
(banana ‘cake tii chicken]
>listinsert(, pasta)
>>list
(Pbanana}past cake tifin chicken]
‘As you can see, a method is invoked by placing a period
between the pice of data that you're applying the method
‘toandthe name ofthe method Then you pass any
arguments between round brackets, jst a5 you would
with anormal function. works the sare wth stig and
any other data object, to:
>> word = "HELLO"
>» wordower)
“hallo
‘There ae lots of ciferent methods that canbe apples to
lists and strings and to tuples and dictionaries (which we're
aut tolook at. To see the oder of the arguments andthe
fulrange of methods avalabe. youl need to consult the
Python documentation
Variables
Inthe previous examples, we used the idea of variables to
mare aber to work wth our data. Variables are away to
name ferent values ~ itferent pieces of data, They make
iteasy to manage athe bts of data you're workingth
and greatly reduce the complexity of development (when
{you use sensible names)
‘As we saw above. in Python youcreate anew variable
with an assignment statement. Fst comes the name ofthe
variable thena singe equals sign followed bythe piece of
data that you want to assign to that variable
From that point on, whenever ou use the name assigned
tothe vail, you ae actualy refering to the datathat you
assigned tit nthe examples. we saw this in action when
\werelerence the secondcharacterina sing or the third
elementin alist by appending indexrotationto the variable
rare You can also see thsin action yeu 300th ye)
function to avarble name:
>» typetnord)
>>read
stypetist>
Other data types
There ae two other commen types of data that are used by
Python: tuples and ctionare. Tues are very sma to
lists ~they'ea sequence data type, are they can contain
elements of mixed types The bg derence is that tuples are
immutable — tats to say. once you create tuple you
Data types
pn@eve=
senseve -18 rvthen
pancakes, ‘sauzage=']
>the Python
corratchnge t-andthatunesareeniedbyround terete
bent as eppoststo sine rset (Hanan; gent loce to
‘creat Ocsonaresaesmisrtoaistr stipe mn pene wih
eyeotanacaectonoeteaitons They nth thon cade an
trelorets antec aro batho andre ae how arnt
creed whch steele
language ditonary The key the word that you'e looking
up. andthe value ithe defition ofthe word
With Python dictionaries however, youcan use any
immutable data type asthe key (strings ae immutable, 00),
solong ats unique within that cctionay. yout to use
anakeady existing key ts previous association i forgotten
completely and that data lst forever.
>> english = (Yess in ber tinux’“operating system)
>> english tree]
“asin beer
> english! ="asin ibery”
>> english ee]
“asin iberty
Looping sequences
(ne commen operation hat yournay want performon
any of the sequence ypesis looping over their contents
toapplyan operation to every element contained thin.
Consider this smal Python program:
Uist = banana tii burt]
foriterin i
print item
Frat. we created thet as we woud normaly, hen we ued
the for. construct perform te pin uncton
eneachitem nthe it.The second worn that constr
oesn' have tobe tem that's just a varabe name that
sts assigned terporaiy to each element contained within
the sequence spected atthe end We could ust 35 wo
row wien for letter word ae would have worked
iustasvel
‘nce you have mastered these sic ata types, you be
ready olook t how youcan put his knowledge to use when
‘modeling rea-word problems in ater ates. nthe
‘meantime. eae Python documentation to becorne
farior th some of he ctr methods tat provide for
the datatypes we've looked at before. You ilo of
set t05 sich 3 srt od reverse
Coding Academy | 11Fundamentals
More Python
data types
Learn how different types of data come together to solve a real
problem as we write some code that counts words
>Haraly
surprisingly.
‘our counting
rogram, after
being sorted,
finds the'to
be the most
‘common word
in The Te
‘common datatypes: numbers (nts and float) strings,
lists tuples and dictionaries. We demonstrated how they
‘work with ferent operators, and explained afew oftheir
‘mest useful methods We ddr, however, ive much sight
ito how they might be used in el situations. n ths arte,
were going tofixthat.
Were going to write a program that counts the number
coftimes each unique word oocurs ina tet fle, Punctuation
marks wil be excluded, nd ifthe same word oceus but in
diferent cases (fr example the and Te). they wilbe taken
torepresent a single word, Fal the program wl print the
results to the soreen. should look tke this:
the 123
yourt0
22600
| ithe previous tutorial. we introduced Pythons most
Asan example, we'lbe using The Time Machine. by HG
‘lls, hich you can dawnload fren Project Gutenberg
‘saving tin the same foter as your Python file under the
ame tmemachine xt,
As the program description suggests, theft thing we
need to do is make the tet accessible from inside ou Python
‘rogram, Thisis done with the apeng function:
flea: is but
tes: came with
ii: so 2 for
11: upon at
2: into flee
iz: ittte me
112: one had
114: not
is: att. it
hie: he that
ii: have ny
21: they in
22: from was
22: there to
129: his a
32: then of
32: you and
37: on i
Si: this the
s7: were
96: time
90: but
12| Coding Academy
‘tm = openCtimemachine.
Inthis example, pend is passed two variables. The fests
the name of the fl to oper: fit wereina diferent rectory
from the Python soit the entire path would have tobe
ven. The second argument species which mode te le
shouldbe openedin: stands or read, but you can also use
wor write or rw fr read-ite
Notice we've also assigned the filets oun variable name.
tm, so we can refer to tater inte program.
With reterencet thee created, we aso need a way
toaccess its contents. There are several waysto do this, but
today wel be using afer. in. oop To ee how this works,
try opening imemachine bet te interactive interpreter
and then typing
>> fo nein tm:
Print tine
The result shouldbe every neo the fe printed to the
screen By puting ths code nto 2. fle, sy emp, ee
got the start of ou Python program,
Cleaning up
‘Theprogrars destin alo specie that we should
exclude punctuation marks, consider the same word but
indierent cases sone werd and tht we're courting
individual words not ines. Ast stands, weave been abet
readonly entice ines as strings. however th punetuation,
strange whitespace characters (suchas Wn) and eerent
cases ct.
Looking atthe Python tng documentation nditat
ttp:/ docs pythonorg/ibrary) we con see that there
are four methods that an hep us corwert ine stags ito
2 fermat closer to that specie by the desertion tp,
‘translate, lower ard split). Each of these re methods,
andae such theyre functions that are app topartclar
Strings sing the dt notation, Fc example stip, which
removes specledcharacters rom the beginning and end of
strings used ike this:
>> tin td
shen passed wth no arguments, itremoves all whitespace
characters, which sone of the bs we needed get done
The function translated) isa method that can be used for
removing 2 et of characters, such 25 al punctuation mars
from string To se itn this capacity trees tobe passed
{wo arguments the fst beng None ar the second being,
the lst of characters tobe deleted
>> line translatlNone, MASKENO* -/s¢=92@1N
dower speaks for tse realy -Itcomerts every character
inating to lower-case. sp spits distinc elementsinside a string ino separate strings returning thern asi
By passing an argument to split is possible to specty
\which character identifies the end of one element andthe
start of another
olin split’)
Inthis example. we've passed. single space asthe character
‘split the string around With ll punctuation remove, tis
\wicrate ast. ith each wordin the string stored as a
separate element. Putalof thin the Python fle we started
‘working on eae inside the for loop and we've made
considerable progres. shoud now look he ths
‘um = openttimemachinett
Because alo the string methods return anew, modified
‘tring. rater than operating onthe exiting string, we've
‘assigned te line variable in each line to store the work of
the previous step.
Uniqueness
Free bak at al ft wrk wee doe vith dt
Byung he smgmetnos webs abetoronoeal
ebiselcoattne wrt rested Wee coset
cookery ereeingata isarabe curbahy
conetiat tonic andntiepeces gato east
sosact einen wee most wore ur
staningpogess nde hells waka
be rene vay rich wets ae
chau arpa sin sb uth nye conan
vat torte
The Wt th tetsu pop ino youd wen
then sbout aquest sacStonay, eheyaestore
wasnt prrarise tout amralenste
{Diy errno nerd esohey whine acre. wee
guorataesture wort bean dupes
‘ats more necance he velo sore th runber
cles tverher scares hewrentngies the
progomcores acess nentstresol sacs
Sorty coat the Scion, arse t
perth aa ot uta ney ace
‘Note sre he start othe oe
ry
Thane ane deters wr eco
ron crworis Not vane oieksboawey oat
coxrwerdina hectare A We saws te ora
trample asaperentstaeret vob reughinada
fen wordt doa We cotter tortor it
teed ate (ose arth fro, tong athe)
‘Govcacteor winovoleofl (ores tht aes
conured on nie)
‘eevee
‘twor
extremes tke eo ests the late s
cseretonon tear set To get ur
can pave aniee douse sie hep
“Another data type
wrestled with, another
step closer to our goal”
More Python data types
Rerun a tof te worden the srg, using sp 0 te delete sting.
most maepat spits we cane us tha bt have most mene ere
ot specie a, then there sh it on the ruber ef Spts (al possbe |
HY sep gen, consecutive deters are not grouped together and are
fevpty stings (for example, “ht-aite Tturne
anak of utile characters (for example, ‘sososaito") Fetus
Spting an emery stg wih a specied separator return "1
tp not specie orf mm eer eng agorthn sapped rn
ferpty sing a a tng conang of ust whitespace Wh a wee Sepa Fo
‘count
sictvord] = count
lee
ictvord)
“Tnsisalite bt confusing because dlet{word] being used
in two diterent ways. inte secondine i returns the valve
‘and assigns tothe variable count. whileinthefourth and are avalable and
Seventh nes, count and are assigned to that Key's how to use them
vale. respectively Notice. oo, that fa wards ready inthe
‘detonary, woincrement the county 1 epresentng
another ccurence
Putting it together
Another data type wrestled vith another stop cser to
‘our goa. At ths pinta that's tt dis insert some code
topnnt te dictionary and putitall together and ran the
rogram. The pent ston shoul look ike tis and be t
thevery endo thee, outside of thelneecpng code.
for wordcount in dct iteritems
print word ++ st(count
“Tri for loop lok ferent to what you've seen before By
Usinatheitertems method ofthe dctonay, we can aonss
both the key (word) and valve (un) n ase
loop What's more ve had to use te tr futon o
‘convert eount. a ntege into a sting asthe + operate
‘antconcatenate aninteger anda sng running it and
youshould see your terminal sereen filed ith ines tke
other: 20
sick
ventilating:2
Data everywhere!
Thats we paedto scents parca or
ats ectioly umoxttobeqate abt As wo shawna
hada chance soo seater typeset dts
the ther thd cabe pedo save area rele
wehope wu ntced hor pratt ose he
aporoprte ype for epresering arent aac
Concepts Aso rhe pewamng exrose hy took
tosoring te est cna ederto se wich
wore cer mst equ
You ase wate cnr wing theres oe
encerttyon atin ose tre tft our bout
Coding Academy | 13,Fundamentals
Reliabili
ab
ty by
straction
Think your code is solid? Perhaps it's a bit too lumpy. Creating
abstractions can make code much easier to maintain
nthe prove tora. ne ve been king.
[zsisrsieamssine tyrone ce sasbes
faveniacarnabetiaat hapa ieptaae a
ssurgare om Toe ose tp we art
‘curator assertion nutetre weston were
rage boom atchacen bot ngromadest
topes topovetrs cu th teeters
tomcat bre hragt rs ral
Square roots
Toget ou heads around the concept of abstraction, a's start
ty thinking about square rts and ferent techni for
finding them. One ofthese was discovered by Newton, ands
thus kown as Newton's method,
Newton's method stats that when tying to find the
‘square root of anumber (x), we shoud start with guess ()
ots square rot we can then improve upon that result by
averaging our guess () wit the esuit of avg the number
(by ou guess). As We repeat thes procedure, we get
closer and closer tothe square rot. in most attempts,
\wellnever reach a definite result, well only make our Ruess
moce and more accurate Eventually, ve'l each level of
‘2ccuracy thats good enough fr our needs and then give
up Just tobe clear about what is volved, take ook atthe
{abe below or how you would appl this meted to find the
square aot of 2 (for example).
ItSalot of werkjustto
find the square oot of
‘anumber. magne if when
you were in school every
time youhadto fda square
root youadto doa these
steps manualy Solveg
problems involving Pythagoras’ theorer for instance, would
be much more unvely.
Luckily assuming you were alowed to use calculators
‘when you were at schoo there's another, much easier
method you can use tnd square roots. Caleulators come
wth button marked with the square oot symbol and all
1 2 @riy2=18
15 AS=1s8 (35+15/2=1467
1467 2raer=1ans [ang +Laer/2=1402
14 | Coding Academy
“This is a very powerful
technique that makes
programming easier”
you have todois pres this button once ~ much easier.
This second approach is what's known as an abstraction.
When working on problems such as those involving,
Pythagoras theorem, we don't care about how to caeulate
the square root. only that we can do it and ge the correct
result, We can treat the square rect button on our calculator
as black box we neve lodkinsde ite dor know how
‘does what does, al that mater that we know how to
seit an that gives the correct es
This very powerful technique, one which can make
programing alot easier because t helps us to manage
‘complet. To demonstrate how abstraction can help us
consider the Python code below fr finding the longest sie
of aright angled tangle
Import math
det pythagla,
a262= (aa) +(0*B)
‘pess =10
while (math fabs\guess * guess) 2262) > 001):
guess =((a2b2 /uess)+ guess) /2)
return guess
“The fist thing to notes that i's not inthe least it readable
Sure with apiece of code tis short. you can read through it
reasonably quickly and igure out what's going on but ata
lancet’ not ctwous, and were longer and writen ike
thisyou'dhave a teri time fguring out what on earth it
‘was ding. What's mre.
woul be very dificult to test
the diferent pars ofthis
‘ode as you go along aka
sncremental development
vita for bung robust
software) Forinstance, how
woul you breakout the code for testing whether or not 2
guess is close enough tthe actual result. or the code for
improving a guess, to check that it works? What this
‘unction et return the expected results —how would you
star testing athe parts to find where the error was?
Final, there's usetl code i here that coud be reused
in other functions, suchas that for squaring a number. for
taking an average of two numbers, and even for finding the
‘square oot a a number, but rane oft reusable because
cf the way its writen You could type ital cut again. or copy
and paste but the more typing you have todo, the more
‘obscure code you have to copy and paste and the mere
ely mistakes are to make tin to your programming
Let's wie that cade again tis tine with some
abstractions to fixthe problems listed above Wehaven't
sted the contents ofeach new function we've crested,
leaving them for yout ili,limport math
of squarele:
def closeEnough guess
ef improveGuessts, guess:
ef aarti gues:
of pythagla, bi:
22 = square) + squartt)
return sqrs2b2)
Here, we've split the code into several smaller unctons,
each of which ula particule. Thishas mary bereits,
For starters haw rach easier she pythagQ funtion to
read? nthe fist ine. you can see clea that a2bis the
result of squaring two numbers, and everthing below that
has been consolidatedin toa single function call the purpose
cof hicis also obvious.
What's more, because each part ofthe code has been
slit intoaetferet function we can easly tet. Fo
example testing whether improveGuess() ws doing the
Fight thing woud be very easy — come up witha few values
for cand guess, do the Improvement by hand, ad then
‘compare your results with those returned by the function,
pythagg itself was found rt to return the corect
result, we could quick test all these aur functions
to narrow down where te bug was.
‘Ad, ofcourse, we can easly reuse ary ofthese new
function. you were nding the square rot ofa number
inadifferet function, for instance, you could just call the
srt0 function - si characters instead of four ines means
there's farless opportunity to make mistakes.
(ne tinal point because our sat code is now abstracted,
we could change the implementation completly, but slong
‘as we kept the function call and arguments the same, all
“This code can be
improved by taking
advantage of scope”
code that ris ont would continue to work property
‘This means that fyou come across a much more ficient
way of calculating square roots, you'enot stuck with
working through thousands of ines of code, manually
changing every section that finds a square reo; you do it
‘once, ands done everywhere. This code can be improved
sti urther by taking advantage of scope. eloseEnough)
Object code
> There ae layers of abstraction underneath everything
you do on a PC ~ you just don't often think of them
s
s
3
Ss
a
a
=
Reliability by abstraction
rt math
def square(x) :
return x * x
def sqrt(x, guess) :
def closeEnough(x, guess) :
Af math. fabs(square(guess) - x) > 0.01:
return True
else:
return False
def improveGuess(x, guess) :
return (((x / guess) + guess) / 2)
while closeEnough(x, guess) :
guess = improvaGuess(x, guess)
return guess
def pythagta, b):
2262 = square(a) + square(b)
return sqrt(a2b2, 1.6)
print pyfhag(2, 3)
> Our fina code
ard improveGuess() ar particular tothe sar functon- for fining the
that stosay. other functions ae unlikely torely on longest side of
their services. atiangles
Tohelp keep ourcodeclean,andmakethereationship longer than
between these functions and sqrt clear we can pace ther what we had to
‘efntions inside the definition of sqrt ‘awk i
‘more readable,
of arts aves: een
ef closeEnoughis, uessi: poser
eFimproveGuessts, guess:
‘These functions are now vsble only to code within the sa)
efntion - we say theyre inthe scope of sqrt. Arything
utsdeof thas na idea that they even ext
This way. we ater nee to define similar unetions for
improving a guess ina diferent context, we wort face the
Issue of calling names or the headache of fring out
what improveGuessi0 animproveGuess20 co.
Layers of abstraction
Hopefully this example has demonstrated how powerful
technique abstraction is Bear in mind that there are
‘many layers of abstraction present in everthing you do
‘ona computer that you never thik of
For instance. when you'te programming do you know
how Python represents integers inthe computer's memory?
Crow the CPU performs arithmetic operations such as
addtional subtraction?
The answer's probably ro. You just accept te fact that
‘yping2 # 3into the Python interpreter returns the correct
result, and you never have to wery about how does his
You reat sa black box
“Think how much longer i would take yout program
ityouhad to manualy take care of what data wet in which
memory location. to werk with inary pumbers,andtranslate
alphabetic characters into their meric representations —
thank goodness for abstraction mt
Coding Academy | 15,Fundamentals
Files and modules
done quickly
It's time to expand your library of functions and grab external data
with just two simple lines of Python
> When you
read a fle, most
languages will
step through its
data from the
begining to the
fend in chunks
you specity. In
this example,
we're reading a
Tine at time
et for before facing the age-old problem ofhow to
set data into and out of your application Whether i's
‘using punched cards to get patterns into 19th century
Jacquard textile oom or Google's bats skimming websites
for data to feed ts search engine dealing with exteral input
isas fundamental as programming ital
‘Adit’ problem anda concep that yourmay be more
fami with onthe Linux commandline. When you ype Isto
lst the contents ofthe
currant rectory for
example the commands,
readingin the contents of a
fhe te current directory
then outputting the contents
to another: the terminal
Of course the inputs and outputs aren fle inthe sense
‘most people would recognise but that's the way the Linux
flesystom has been designed ~ nearly everything safle
This Relps when you want to save the output ofa command,
cor use that output asthe input to another.
‘You may areadykrow thet typing is ist ill ecrect
the utput rom the command toa fecal iste, but
you can take this much further because the output canbe
treated exact thea fle. 1 | sort-r ill pipe that's the
vertical bar character) the output os into the nut of sort
tocreate a reversed alphabetical ist of flde's contents
Fe: majority of programming projects. you don't
16 | Coding Academy
“If the filesystem knows
the file is being changed,
it won't allow access”
‘The complexty ofhow data input and output can be
complished sentiely down to your programing.
cevcreent Every language villnclude function t lead
‘and save data. forinstance, but this can ether beet or
‘25y depending on how many assumptions the language
‘sing to make on your bea However, there's aways
logical sequence of events that need to occur
‘You willis ned to open aie, creating one iit doesn't
‘stand then ether read data rom ths fle, or write data to
Ibetore expty closing
the le again so that other
processes can useit
Most languages requre
youtospectyaread mode
when you opena file
because this tls the
fiesystem whether to expect le modifications or nt
‘Thisis important because mary diferent processes may
‘also want to access the fle, andi thefesystem kno the
fie is being changed won't usually alow access. However,
many processes can access areaxonl file without needing
ta worry about the tego the datait holds, because
nothing sable to change It you know about databases,
its the same kindof prblem you face with muliple users
2ocessing the same table
In Pytnon as with most other languages. opening. fe to
vite ors read-only canbe dane with a sing ine
pen(st.xt =r)
Ifthe fle does exist, Python vill generate a"No such
feo directory" error Te avoid this, we've used the output
‘tom aur cornmand ine example to create atetlecalled
ste. This is within the folder from where we launched the
Python interpreter,
Environment variables
Dealing with paths, folders a fle locations can quickly
become complicated, andt's one of the more tecous issues
youl face with your own projets Youl find that ferent
“envorments have different skations for finding fs, wth
some creating keyword or common locations and others
leaving tothe programmer.
Tis tn'so had when you only dal with fs created
ty your projects. butt becomes ficult when yeu nee to
know where to store aconfguration feo load a detat icon,
‘These locations may be diferent depencing on your Linux
lstriaution or desktop, but witha crosplatorm language
suchas Python, theyaso be diferent foreach operating
system. For that reason, you might want to consider using
ceaveonement variables, These are simi t variables atha gobal scope in many programming languages but they
analy to any one user's Linx session rather than within your
‘oun code. you type envon the commend line, for instance,
youll see ito the environmental variables current set
for your terminal session Look dosely, and you'l se a ew
‘that apoly to default cations and, most important, one
called HOME The value assigned to this environmental
variabe willbe the location of your hore folder en your Linux
system, and fe want to use this within ou Python sori
weirs ned to ad aline to import te operating ster
spect module The ne to do this i:
import os
This command also opening ale, but notin the same vay
we opened lst xt This les known as a module in Pytnon
terms, and modules tke ths impor functionality. incu
statements and definitions, so that programmes doesn't
have to keep eventing the whet,
Modis extend the simple constructs ofa language to
add portable shortcuts and solution, whichis why other
languages might cal them ibrar. Libraries and medules
area ite ke copying and pasting someone's ov research
and insight into you onn project. Ont’ better than tat.
because modules such as 9 are used by everyone, tursng
‘the way they do things into a standard
Setting the standard
“There are even bares cal tad these embed standard
ways of eing many thangs language doesn’ provide by
deat, such a5 common mathematica unctons, data ypes
are sting services 35 wel 3 fle nput/output od support
for speci ie types You wl find the documentation or
what ibary does within an AP. This willis each urction,
what oes. ana what requires a an put ar an out.
"eu shoud aso be able to find the source es used the
impo (ancy inetd othr language). On most Linux
systems for example. ib/eython2x wilt athe
modules. fyouload espy it ate eto youl see the
code you've just added to your project. a well as which
functions re nom acessibe to you
“There are many, mary iferent modules for Python ~its
ene the best reasons to choose over ary ater onguage
and more can usualy be installed wth usta coupe of cloks,
from yur package manager
But his wher the uly
spectro dependencies can
start tohave anetect on your
project because you want
{ogneyour code to someone
else, you nee to make sure
that person has also got the
samemodules instal
ityou were programming in Gor C+ for example. where
‘your codes compe ad ke against binary rare,
those binary lars wills ned tobe present on any
other syste that runs your code. They wibecome
dependencies for your project. which is what package
‘managers do when yu instal a complex package,
The os module
Getting back to our project the os module designed
to provide portale way of accessing operating system
dependent functional so that you can write mut platform
applications without worrying about where files should be
laced, This includes knowing where yourhome directory
ght be. To see what we mean, add ths code:
“If you load os.py into a
text editor, you'll see the
code you've just added”
Files and modules done quickly
> Binary files
= openfostnvronHOME"I "ist ") hhave no contest
‘Ths ine will open the fe stein your home folder Python without an
‘esociated fle
knows which home folder i yours because the eseviron
unetion fom the es module retums a sting
‘from an erwironmental variable, and the one we have asked handling therm.
MoreumsHOME Butaveveconelsonn thei WM wy 308
SeveneyeteatawattscortetsThsmgttecm gether ata
Coutectate batts onhtreltrowacktotheway
tha files usedto be stored, whichis why this i alo the way
nearly al languages work ts ony ater fle as been
‘pened that you can start to read its contents:
readline)
The above instruction wlread a single ie ofthe text
fie andoutput tis othe interpreter as string. Repeating
‘the command wil rea the net ine, because Pythons
remembering how tar through the fe thas read,
Internal this is being done using something called a
pointer ands, too, commento the vast majo of
languages Ateratively you wanted to readthe entire
fle, you coud use fread, Because our le contains only
text copying the cantents toa Python strings an
easy conversion
The same set tue of
binary fe. Rather than
being treated as tet. the
organisation of the bits
and bytes that make up
binary fe are organised
according tothe fle type used by te fle ~ orne filetype at
lif’ raw data. Asaresut Python or anyother
programming language) would be abe to extract any
context roma binary fle, causing an eter youtry tread it
intoa string, The soliton, atleast forthe nal np isto
‘adda ag when you frst open te le, because his was
Python to expect raw binary. When you then try to read the
input youl see the hexadeciral values of the fe ouput to
the cspay.
“Tomake tis data useful youll need todo some extra
‘work, which welll at next but rst, make sue you close
the open flea this should ensure the integnity of your
‘system. AS you might guess. the command looks tke this:
close
‘Andis a5 easy as that a
Coding Academy | 17Write your own
UNIX program
Try re-implementing classic Unix tools to bolster your Python
knowledge and learn how to build real programs
ringto do: get Our galforthe pro
you writing eal programs O
to create 2Py nents accepts user input onthe standard input pi
Popular Unie tool eat. Like all Unixtools. eats agreat target _unblan end of ine characteris reached, at whic pont it
because it's smal and focused ona single task while using
the net few tutors, Create aPython program. eat that when called
I nthe next ew pages, that's
imelementaton ofthe
vealed wth fe names
features, nclusing access
gurmentseatpy should
reach ine ofthe fle to
standard utp,
“You now know more eof the
than enough to start ee
writing real programs”
your chosen lang aching and which
vith ral work number a the beginnn
i put $ signs at the end of
wecurrent ine
th any numberof les pas
the command in
you want foo along, mak
version, because some fet
Python2,
Python files
Let srt ithe ess part ofthe robles
cortorscfafe ney ne tostonarsou nF
fom thervse mene
pentheltat"")
this, not write t because
suent to the open function.
ould be opened in reae-oly mode
0 the flenow provide through the ney
the nent tasks to dsp
tyne on standard output. This
because in Python fi eobjects
her tuple
ddctiona 2 ther navi member
fie this
puting tina fri
forling in fil
Printing)
function then
atever argument you pass
output
The final program we'll be implementing. I's not long, but it makes use ofa ot
‘of core language features youl beable to re-use time and again
18 | Coding Academyyou put al hisin fle, make t executable a create
hella tien the same drectory, youl see that works
rather wel There is one ay, however ~ there's an empty
Ine between each ine of output
“Te reason this happens shat pent automaticaly
ads a newine characte tothe endaf each ine, Because
there's aready a newline character a the endo each ne
inhellatet tree's, even ityoucant see it otherwise
everything woud be on one line), the second newline
character leads oan empty ine.
‘oucan fx this by caling print with a second named
argument such as: printing, end=™) Ths tls pint to
put an empty string oF no character at the end of each ine
Instead of anewine character
Passing arguments
Ts alr compared the elem command
thanisa glaring onion bre we wad hae teh
progr crete to change wih fe sing apo
toetandad au Wt we neds some wey topos
arguments onthe conand ine, otha we aclu
new program y pine ety halla one cormars
ine Se Python as‘ batteries cused! thts a aly
Slgionerd ask 0 el
The Python terpeter tora captures all
angers paseo the coneand nm arcane
Caled sys wchis part ofthe Standard Libr. mates this
vail to your cage
Een though ay part te Standard bray itt
avaible to your code by deta nad you ts a to
impor to your program andthn access coer wih
cetretaton cont wary wT expan Fis nomoment
Fist trnprtt to our rogar s
ine oy
sothetopof your ety ie
The part he eye mode that wee interested inthe
ang object. Tis odyet stores ofthe arguments passed
cre conmand nein a Python it wih rneansyoucan
tome arirmripdatn tuo vrntecvigues vse
seenin prev or nd show hue ones
Ther ae ony two tres
youreatynedtokrow
oaths Ty are
Doh rs lamer of the
fats the nae othe
prewar sat- all
aramerts oo is.
2 To access thelst you needto use dt notation thats
tosay ang sstoredvithinay. 500 aces you ee
totvpe sysang or sysarzvl ose the rst puerto
your roger
Knowing ths youshould ow be able toast the cade
we ented prey y replacing bala wth arg
(When you call etpy on te comenae ine. yo an fen
poss the name cf tet le ordi werkt te some
Many files
Ofeoute our our smart toacept mor than eee
andaututal hr cote to sanrd out one oer
anata tes tgs san our pegam eo at
crefiessahorgamert.
Totti pra problem you ead oop rahe
tes re ng st Thon ttt youre be cae
brnenyoudothslstayoerae te ey rs comer,
“The part of the sys
module we're interested
in is the argv object”
Write your own UNIX program
> The output of the real Unix command, cat, and our Python r-implementation,
‘are exactly the same inthis simple example
because thi the name of the program sal fyou think
back to our previous article on data types and common
list operations. youl realise hiss easly done with a soe
This is just one tine
for file in sysargit
Because operating
onal the fs passed as
arguments toa programs
sucha common operation,
Python provides ashorteut
for doing thin the Standard Library called fleinput
Inorcer to use this shortcut, you must ist import by
putin import fleinput 2 the top of your code You wl
‘then be abe to ust to recreate the rest of our ea program
so a 28 follows:
{or line in filsinputnput:
Printine, end
This simple shortcut function takes care of opening
‘2ch'ilein urn and makingll ther nes accessible through
angle iterator.
Thats about all that wehave space forin this tutorial.
though there has not been much code in ths particular
‘example, we hope you have started to get a sense for how
‘much savalablen Python's Standard Library (and therefore
how much works avaiable fer you to recycle). and how
good knowledge ofits contents can save you alot of work
‘when implementing new progres.
Coding Academy | 19Fundamentals
Enhance your
UNIX program
Our tour of the Python programming language continues, as we
continue our clone of the Unix cat command
he previous tutorial showed you how to bud a simple
‘eat cone in Python. In this guide, were gong to acd
some mere features to cur program inciuding the
abt to rea rom the standard input pipe, just ke the real
cat and the ability to pass options to your eat clone, So,
without further delay, at's vein
Fortunately, you aeady know everything youre to
interact with the standard input pipe. In Linux, allpipes are
treated ke files youcan
pass ale as an argumer
‘command or you can pe
pipe as an argument. It
doesnt matter which you
o, because they basically
the same thing
In Python. the same itu. Allyou need to get to work
with the standardinout pipes access to the sysibrary, which
ifyoufolowed along last time you aleady have Let's write a
Inte sample program fist to demonstrate:
impor sy
forline in syestin
printine,end=™)
Theft ine imports the gys module. The nes that olow are
almost identical to those tat we had last time. Rather than
spectyng the name of a file, however, we specie the name
of he le-object. stn, which sound inside the sys module
Jus ike areal len Python the standard input pipe ts an
erable object. owe use afr lop to walk through
‘ou might be wondering how this works, though because
standard input stars off empty tyourun the program. you
will see what happens Rather than printing out everything
that’s present straightaway t wil simply wait Every time a
newline character's passed to standard input (by pressing
to
“Python provides us with
amuch more powerful
alternative to sys.argv”
[Retur,twilthen print everything that came before itto
standard output
Right, now we have to modes that our program can
‘operate in. but we need to put them together inte a single
program. fe call our program with arguments, we want i
to worklke las time ~ thats, by concatenating the fles
contents together: it's called without ary arguments, we
‘want cur program to work by repeating each line entered ito
standard input. We could
easy do ths with what we
have learned so far ~simaly
check to see what the length
ofthe sysargv arrays Hits
‘reat than L then do last
lessons version, otherwise
do this version:
iflenaye are >
last month.
lee
[this month.
Pretty straigtforwaca The only point of interest heres the
se of the len function, seeing 2s were on aourney to
discover different Python functions. This funtion suit into
Python. andcan be applied to ay type of sequence cbect (a
string, tuple or st) or aap (ikea detonary). and it tay
tells you how many elements ae in that abet,
There are more useful functions ike this, which you can
‘ind at http://does python.org/3 library functions html
Parsing arguments and options
“Tite spit appronch, however. and Python
Ectily prondes swe ch more owetlcReatheto
fysarg To demonstrte ths eae ging tad two
pinto or progam that al ody ct erat
byour progam
You may rote relsedt, bt eat does inact havea
ange of opto Wega to plement the which
‘Hows dolar srs ath ndefnes adn hich
Gol ine rues the bepangf es
Toots wel start by sting upon OpionParer, This
ssa specal object provided os part te eptparea mode
whch wild most of thehard work br you Fs wala,
nica) datecng ots ders owerayoom
lt of hor werk OptonParser vil automaticaly ponerse
heb tod foryour srs ert a hey us your
prowam incorrect reas help hats
> The Python language comes with all the bells nd whistles you need to write
"useful programs. n this example, you can see the replace method applied to
2 string inorder to remove all white space ~ in the tutorial, we used the rstrip
‘method fora similar purpose
Ujone@tT04304 -18 cat py help
Usage:catpy [OPTION}..[FLEL
Options:
20 | Coding Academyh,~-elp show this help message and ext
-E Show $.at lineendings
sn Show tine numbers
Just ike areal program! To get started with OptionParser.
youtist need to import the necessary components
{rom optparse import OptionParser
‘You may notice tha his looks bit erent fom what we
saw before Instead of importing the entre module we're
only importing the OptionParser object Next you need to
create anew instance of the object. acd sorne new options
foritto detect wth the adoption method, and passit a
usage string to splay:
usage ="usage:%pog option..."
parser = OptionParserlusage=usage)
parsoraddoption-E7 dest="showend action="tore
‘rust help="Show Sat ine
cencings?)
parseradd_option- dest="shownumaction="store_
‘rue’ help="Show ine number”)
‘The Moprog port ofthe usage string wil be replaced withthe
‘ame of your program. The dast argument species what
fname youl be able to use to access the valve of an
argument once the parsing has
Enhance your UNIX program
>The Python 3
website provides
‘icelent
‘documentation
{for aweaith of
builtin functions
the code that will run when a particular options set. noth
cases, wel be measting the string of tet that's output by
the program, which means you need to know litle it
about Pythons builtin sting editing functions Let's think
about the-E or showend, option rst. Allwe want ths todo
Is remove the iisibe ne break that's atthe end of every fle
(ex every line of the standard input pe, as implied by
pressing [Retur), and replace it witha dla symbol
folowed by aline beak
The fst pat removing the existing new tne, can be
achieved ty the stringestripO method. Ths removes a
whitespace characters by default atthe righthand
edgeofasteng
you pass astring tt as an argument il stip those
characters fom te righthand edge instead of whitespace.
nour case, just white space wll do,
Completing the job
“The second part of the ods 35 simple as seting the end
variable in the print statement othe sting $\n and the job
is almost complete. We say “almost complete because we
stileedto wit some more loge to further controthe ow
‘ofthe program based on what
Sicwatatcs” {| “Don’t confuse yourself Shs "fscsy wamens
cekMmeacese | Dy putting the variables "92 au.
todertwasstomeen § the other way around!” — wes iobssitiere
‘arguments present. ond False
iftis nt. You can read about othe actions at
htp:/ docs python.org/3 /ibrary/optparse htm
Final wth everything set. you ust need to parse the
arguments that were passed to your program and assign the
resus to two array variables
(options, args) = parserparser_args)
‘The options variable wil contain all user-defined options
ssuchas-£ orm while args wil contain al positonal
‘arguments lft ver after parsing out the options. You can
call these variables whatever you tke, but the two wil bays
‘be setin the same order, 0 don't conse yourself puting
the variables the other way round! With the argument
parsing code write, you next wantt start implementing
‘complicated than it orinanly
would be because we need to maintain a cumulatwe count of
lines that have been printed as the program un to
implement the second-n, or showmum, option
Wii there are several ways you coukl achieve ths, the
next tutorial wee going to introduce you toa bo objec:
oriented programming in Python andenplement this
functionality in cass.
Wel also introduce you toa very important Python
convention =the main function and the name variable In
the meantime, you can keep yourself busy by imestigating
the string format() method and see whether you can
figure out how you can append anumber to the begining
ofeach ine.
Coding Academy | 21Fundamentals
Finish up your
UNIX program
Our guide to the Python programming language continues.
This tutorial, we're going to finish our & lone of cat
Jevecomecute orgy ar helt 0
VW iscsstoemeneteny
cote
sting stot er
treaty nerhostada
patos ade
‘Big oct and tp
cptes passed ese ot
Seeeen Atharener
torus torpemat hte uber pon ano
tether gat vying ob we even tos
Sovwntteraren
Objects
Last tine we ended saying hare are many way 8
could gent ee costrg opten meu per
Wereorgtoshow youow tocb itn anobectaneted
She teense tgwes won ecse ovo outs
deoect offen pragranring You ca howe tha
“It’s a very natural way of
thinking because it
mirrors the real world”
ltl careful thought implement the same function with some
nested for oops, anough they re not nearly as redable as
object oriented code
‘When bul complicated
programs, fguring out howto
‘organise them so they remain
2s) toread, easy to track
‘which vanables ae being used
‘by which functions, and easy
to update, extend or add new features, canbe challenging
Tomake ths easier, there ae various paradigms that provide
‘techniques fr managing complet
‘One ofthese paradigms the concept of object-oriented
programming, in object erent programming the elements
‘ofthe pragram are broken down int abject which contain
sate variable, nother words = that describe the current
Concition a the abject, and methods that allow us to
perform actions on those variables or wth tat object.
tes avery natural way of thinking because it mirors the
real worl so closely. We can describe a set of properties
‘bout your hand, such as having fve fingers that arein
‘certain locations and we can desenbe certain methods or
things yeu can da with your hand, suchas moving one fier
to press akey. orholdng a cup. Yeurhandis an object.
‘complete with state anc methods tha lt you work wth
We're going to turn our eat program into an object where
its state records how many ines have been displayed, and its
methods perform the action ofthe ea program ~
edsplaying le contents tothe screen.
Python objects
‘than implements objects tough acs system Acasis
tum, andan object a parce stance ofthat as.
‘redaedon tetera Me tine aw clas with
Skeyord much ike we defnea new function
clas catommand:
Inside the cise otc the methods (unto) an
lata that we want to asciate with eerste othe
sje Thr are som spec methods However that are
freq used One ofthese thei etd sis ran
wen the ass rst nstantated eto partiour cet.
dala yout sat pectic arable ta you watt
trlora that obec.
def int
Selteount= 1
Intiscase weve assigned tothe count varia wl
betning ths recordhow many nes ate een depo
Yeu proba noted he sel variate seed asthe frst
xgumentt the method. end wondered what on ert at
> dust to prove that it work, here's our eat implementation, with al ofthe
‘options being put touse
22| Coding Academywas about. Wel ts the main distinguishing feature between
‘methods and ordinary funetons.
‘Methods even those that have nother arguments, must
have the self variable ti an automatiealy populated
‘variable wich wilalvays point tothe particular instance of
the object that you're working with. So selfcount i count
‘viable tats exclusive to indvcual instances ofthe
catCommand obit
The run method
Werext ned to writea method that wil eco the
appropriatelogic depending on whether certain options ae
St ee cal thst um metho
def rune options
fet dau options
for tine ini:
‘imodiy printed line according to options
ifoptions showed
(last time)
‘options shown:
line = (0) 1)"frmat(selécount, tne)
setfcount +
rintie,end=2)
Notice that weve passed the self erable to tis method too.
‘The two other arguments passed to this function re
arguments that well pass when we all the method later on,
[ust ke witha normal function. The fst. Lis ging to bea
reference to whichever fils beng displayed at this moment.
le the options variable a elerence to the options
‘decoded by the OptParse module.
‘The logic ater thats clear foreach nein the current
fle, city the ine depending on what options have been set
Ether we do as lst tine, anc mexiy the end character tobe
*$\n" or we mod the line, using the format method that
we suggested you research lst time, o append the count
‘variable defined inthe lnethod to the est ofthe ine. We
thenincrement the count and print thee
‘The mestimportant partis the useo sl. Iletsusreler
tovvariabies stored within the curent instance ofthe object
Because it’s stored as part ofthe objec wil persist ater
the current execution ofthe run method ends. As ong as we
Use the an method attached othe same abject each te
weeat anew fe inthe argument ist, the count wll
remember hon many ines were displayed inthe last fe, and
contin to count carrecty
might seern more natural
~ ven the description of
‘methods as individual actions
“The last thing to do is
Finish up your UNIX program
> The completed program isnt very long, but it has given usa chance to
Introduce you to many different aspects of the Python language
penta")
cru, options)
ee
ccrunys.stin, options)
‘We haven filed in the object parsing code from the previous
tutorial because that hast changed. Whatisnew shee =
‘catCommand ine. Tis show we create an instance of
lass ~ how we create anew abject. The ecbect naw has a
‘arable, count. which is accessible by llits methods asthe
‘seltcount variable This 's what will enable us to track the
Ine rumbers.
We then check to see whether ary arguments have been
passed Ifthe have, then we cllthe run method of the
‘object for each fe that was passed as an argument
passing inany options extracted by OptParse along the way
Irthere werent any
‘arguments. though. we
snply cal he run method
Satna call the main function shamans
Srgument int a diferent when the program runs” ‘The fal hing weneed
‘method. and this fine way
ta approach the prablem,
‘Te reason weve donet this way, though s that we
found it meant we could re-use mare code, making it more
readable and ess error-rone
‘Nowallthat's et todo i tote everything together. We
are going todo this by wring aman function This isn't
actually required in Python, but many programs oll this
Ichom, so we wil too
det main:
[option parsing code.)
todohereis actus cal
the main funetion when the program iru:
fname 22" main
main
These ast two lines are the strangest of al. but quite useflin
alot of ereumstances. The name variable especial when
the programs run on the cormmardiine.oratherwise asa
standalone application itis eto male; however, when
itsimported as an external module to other Python
programs i’ nt
Inthis way. we can automatically xecute main when run
‘as a standalone program, but not when were importing as
‘amodule.m
Coding Academy | 23,Fundamentals
Neater code
with modules
Untangle the horrible mess of code that you've made and add
coherent structure to your programs.
“As your programs grow in
programing isall about managing complesity and
we veintroduced yeutoqute a few af thetols and
techniques that help programmers do this. From variables
te function defintions or abject orentation, they all help
(One tool we've yet to cover n pat because you doit start
to come across tun you're witing larger programs.is the
idea of moles and name spaces.
Yeti youve wien a program of ary length, even just
fen hundred lines, this tol youre no doubt desperate
for naleng fe of code, youl havenoticed how quickly it
becomes more ficult to read Functions seem to blur into
‘one another and when you're trying hunt down the cause
‘ofthe latest error you fit etc to remember exactly
‘where you deine that aimportant variable,
These problems ae causedby a lack of structure. Wiha
your code ina single ie. is more ecu to determine the
‘dependencies between elements of your program ~ that
\wbich pats rely on each other to get wore dane ~ andt’s
more dificult to
sualie the fw
| our previous tutorials, wehave mentioned how
fata though
length, there are other Ti
problems that occur” programs
Brown length,
too, there ae other problems that begin te accu: or
instance, you may find yoursel with nang confit, 3s.
two cert parts of your program require functions calles
‘add (for example acing integers or adding actions in
_amatheratics program).or you may have writen a useful
function that you want to share wit other programs that
youre weting, and the enly too you have to do that i boxing
‘and ertor prone copy and pasting
Untangling the mess
Modules area great way to sohe alot these probems,
nabing you to put structure bck nt your code, ting
you mod naming confit, rd making easier or outa
share useful churks of code between programs.
"oul no dou been using them theme your code
_syouive led on Python buat nr the party modules to
provdelots of extra furctonaty AS an ample, remember
{he optparse mockle we used before We clued tin our
program along wth the import statementike so:
importoptparse
_Aer puting ts ine atthe top of our Python prosram. we
magealy got access toa whole loadof othe functions that
‘automaticaly parsed commande options We could
24 | Coding Academy
access them by fyping the module's name, flowed by the
ame ofthe function we wanted to execute:
‘optparse OptionParsert)
‘This was great roma eadabilty perspective n our eat
clone, we didnt have to wade through ots of cade about
parsing command ine arguments; stead we coud focus
‘onthe cade that deal with the lage of echoing fe contents
tothe seeen andto the standard output pipe. What's more,
we dt have to worry about using names nour own
program that might colle with those nthe optparse
‘mdul, because they were al hidden inside the optparse
namespace, and reusing ths code was. as easy as typing
Import optparse no messy copy and pasting here
How modules work
Morkdes sound fancy and you might think theyre
ccompcated but -in Python atleast ~they'e realy just
plain odes Youcan ty itaut or yoursel Create anew
‘rectory andinsde ereate a factpy fle, Insde it. deine
function to etu the factorial of given number:
‘def factorial
result =1
whiten 0:
itn==1
result
lao
result *=
n=
Create a second Python fle called doMatapy Inside this
firstimport the modkie you ust created and then execute the
factorial function, prtng the result tthe sreen
import tact
print fact factorial’s)
"Now when yourun the deMath.py fle, you should see120
printed on the screen You should notice that thename
‘ofthe madul is just the name ofthe fe, inthe same
rectory with the extension removed.
We can then call any function defined in that module by
‘ypingthe module's name, folowed by adot, flowed bythe
function name,
The Python path
Tebgeston hase how oes yon now where
{bloke your nae?
Tre aoe tat Pho as re dete set
lcatoathtteesinfo te testat math fe nae
spect nyourtmpot ates ist os nce alt
Srebuttin mss elector of wha ceed whenNeater code with modules
your insta Python then searches through alist of
dlrectores known asthe pat
That s much he the Linux Bash shes SPATH
ceavconment variable ~ tutes the same syntax, an
serves exactly the same function. Ivars, however in how
the content ofthe Python path ae generated Intl
‘thelocatonsstoed inthe path cans of teflon
two lations:
The rectory containing the script doing the importing
The PYTHONPATH, which sa set of ciectories pre
detinedin your default instalation,
‘ou an inspect the path in your Python environment
by importing the sys module ar then inspecting the path
attribute typing sysipath wll do te rick). Once a program
has started, can even modky the path tse and add other
ications toit
Variable scope in modules
Before you head off and start merry wring your om
modes, there is one more thing that youneed to know
about variable scope.
We have touched uoon scope as a concept before, but
_aquickrelresher, scope refers tothe part ofa program from
which particular variables canbe accessed, Fr instance,
a single Python module might contan the fling code
food = apples’ oranges’ "pears"
whe many peopl think Python as
toformat your coset
readable fo co worker
your tr a)
youre intrest
there are to vary use
2g actualy been
Srundsnce the early 1990s. As wth
ary prograrenng language thats been
aroun for any length f tine people who
often have learned ot about he
waysto do ings in he langues
By splitting
your code up
placed inits
ixf-example
rectory, you
‘an bring order
to your projects
and make future
‘maintenance
aot easier
print food
ef show chocd
food = snickers
print food
show cho
print food
you ran that you see that outside the function the variable
food refers toa isto ut, whl inside the uneton. refers
toa of chocolate bars, This demonstrates to diferent
ne global scope of the curent module. in which food
that “iry milk]
scopes
refers toalisto rut and the local scope of the function.
which foodreters toa tof chocolate,
When loking una variable, Python starts with the
innermost variable and wors its way out starting wth the
Immediately enclosing function, and then ay functions
enclosing that, and then the module's global scope, and
hen finaly it illook at al the buiin names.
n simple, single programs, iis a bad ea to put
variables nthe global cope can cause confusion and
subtle erors elsewhere in your program: Using modules car
helo with this problem, because each medule has ts own
bal scape. As we sar above when we impart a maddie
its contents areal stored as attributes of the module's
ame, accessed via dat notation. Ths makes global variables
‘somehat ess troublesome although you should stil be
rake suet itp: /python.net/“goodger?
sandanyere else projeets/pycon/2007/idiomatic/
anyouncuting —Randouttmt
rn python ong /dev/peps/
infecngout mere pep-0008
ead thse and youre sue togain
some deeper sight int the arguage
Coding Reademy | 25Fundamentals
Embrace storage
and persistence
Deal with persistent data and store your files in Python to make your
programs more permanent and less transient
tras chee cebu 5008 ae
Se 5 ts dys wo wen
Smarhores come wha est 8 of sora nd
raryareasyerpndleup 128 fr ene pre
tapos
ano srr tan tat aest vy rode
sopaon ce anor ay or oe arts
Srterten dea coed asp aes
‘Sed ges too itor pets Tels goes nao
Within prong tos grt
dernastatato ode wiper tno a.ngs
otehoce Pon
Ths cine eprint ch
tate aragonite storage appr Ris
ined intr sacra anyone a
Inouye totae asorage
pepe rms nbc wan cry hat
ver ourane yan eta omer 9e
wr oheryoulahodte arash she
open fino
“Almost everymodern = eee"
application storesdata e"..
iste tena
in one way or another” —stwsiensm
spectes which mede the fle shouldbe opened nin this
case i's write, but other valid options include read-only (F)
and append (a).
Inprevious tutorials, we've shawn you that this fle object
isin fact aniterator, which means you can use the inkeywerd
toloop through each nein the fle and deal wth its contents,
one lineata time, Beoce reviewing tat nfrmation, however,
let's look at haw to write data ta fle
Writing to files
Suppose you'e wrt your own RSS application to deal
with your favour feeds. You've already got some way to ak
Userstoenterina is of fees (perhaps using aw. Input,
or perhaps using a web form and but now you want Yo
Store that st feeds on disk so you can re-set ater when
yeni checking for new updates. tthe momen, the feds
dre et storedina Python it
foods = "htp/newsss bbe.co.k/ssinewsonineok.
eition/rontpagtssxmt http: trradarcoms"]
Toget te feds ta thefe is asp process. dist use the
write metho
{or feed in feeds:
filet YO" format ond)
25 | Coding Academy
Easy! Notice how we used the format string function
‘aad anew ine to the end ofeach string, otherwise we
would end up with everything on oneline ~ which would
have made it harder to use later,
Re-using the contents of ths le would be ust as simple
Using the file 2s an teratr oad each in turn into st
‘ripping ff the traiing new ine character Wl eave you
terfigure this one out
When using flesin you Python code there are two things
that you need to keep in mind, The fists that you need to
comert whatever you want to write othe letoa sting rst
‘This is easy, though because you can ust use the buat in
‘strQ function — for example ste(A2) => "42",
The secondis that you have to cose the fle ater you have
frished using it~ you dono this yours lasing datathat
youthought had been committed to disk but that had not
yet been Rushed. You can do this manualy wth the else
methodat fle obec. In ou exampl ths woud translate to
‘adding fleclosed) to our program: I'sbetter however, to
vee the with keys
with opentf-tet.bt a) a le
foods = line.strip( "for tine inf
This simple piace of Python code has opening the
fie object and when the blockinsde the wth statement
isfnished, automaticaly closest fr us, 5 wel you are
unsure what the second line does try looking up Python Ist
comprehensions ~ they'ea great way to write tficient,
concise code, and to bringa modicum of functional style
into your work.
Serialising
tring wits woud bemuchcsertyoudirithve
{pwery au comerg art (or deboay. t et
inti) toa sgt Scere
Toco et mesy orate Pinon poe oto
tomate tho enoer rus
Theft hse ote plea mode Pek
accoptsnry fleet nds tte cts ae ean then
Smet eto achat rg and apa, uo
hv o dhe Ne pein eg bayoura re
hove to wey abu tqug ten propa sg
reese your cee
Topol
with open f-test.b "a as fle:
Pickle dumpifeods file)
with open bf-tet.b
feeds = pickle oad(S zone
Table Of Content:
Overview
Embrace storage and persistence
ZODB - a native object database for Python
Dont squeeze yourctets ito tables sore tein an objet database
Pyton programs are witlon wih he ctoct aera paradigm. You use jects that reference each ot
haste adhereto a specc schema and can hol arta internation
‘String those objects in reltona dlabases requires you fo ve up onthe feedom cf reference
reduces your aby own object reted code
The Z0D8 is nave object database tat stores yur ejects while alowing youto wot wih ay paraq
cade becomes sirpe, moerobust and ease fo understand
‘Also, teres no gap between the dalabase an your program ge codeto wit, ro mappingsto co
Some a he fetus that ZODB rings to you
+ Transparent persistence for Python objets
+ FuIAGO. compe ansacton sap (ncusng savepos)
* Historyundo abit
fice suppor fer binary large ebjers (BLOB)
+ Pupil Sages
> It you're intrested in persistent data in Python, a good next stopping point isthe ZODB object database's much
easier and more natural in Python than a relational database engine (www.2odhorg)
‘This is much ease and thas other applications outside of
persisting data nfs. too. For example you want
transfer your fed st across the network you would rst
have to make t into character string oo, which you could
co with pickle
‘The problem with thsisthatit wi only workin Python =
that's to say. other programming languages dont support
the pickle data format you ke the concept of picking
(more generically serialising) there's anther option that
‘oes have unport in other languages, to: JSON,
‘Yu may have heard of SON — it stands for JvaScriot
Object Notation. ands way of converting objects into
human-readable string representations wich look almost
identical to objects fund in the JavaScript programing
language t's great because it’s human readable, and so
becauseit's weely supported in mary ciflerentanguages,
largely because thas become so popular wit fancy web
20 applications.
Inython. you use itn exactly the same way s pele — in
the above example, replace pickle with json throughout. and
youl be wring interoperable, srased code!
Shelves
Ofcourse. some cede bases have mary diferent abects that
you wantto store persistently between uns, and keeping
‘rack of many ferent peed fs can get rey. There's
rather Python standard module, ome that ses pele
\eiderreat, butmakes access tothe stored objects mare
intutive and corwertent the shelve mode.
Essent shelf a persistent cctonary thats to
say, a persistent way to store key-value pas. The eat thing
about sheles honever that the valu an be any Python
cbject that pee can serials. Let's take afook at ho you
canst Thinking back ocr RSS reader appliation,
imagine that aswel the ist feeds to check, you wanted
tokeep track of how many unread ites each feed had, and
‘which item was the lat to be rad, Your this witha
‘dictionary or example:
tracker = {"bbecouk": ——("last-read” “oot
‘num-unread”: 10
inucormatcom': (last-read""ar
“num-unread”: 5,3)
‘You coud then store the tof feeds andthe tracking deta
for eachina single fie by using the shelve medul,
he so
import shelve
shelf = shelve open("c-test”)
shetffeeds"| = feeds
shelftracker"] = tracker
shetéclosed)
“There ae afew important things that you shouldbe aware of
about the shelve module
>The shelve madule has its oon operations for opening and
Closing fies so you can't use the standard open function,
To save some data tothe shel, yourmust frst use 2
standard Python asignment operation ose the valve ofa
particular key to the object you want o save.
2»As with les you must clse the shel object once rished
ith otherwise your changes may not be stored
Accessing data nse the shes just as easy Rather than
assigning key inthe shel detonary toa value, you assign 9
‘vale to that stored inthe dictionary ata particular key: feeds
= shol[“feeds"] Ifyou want to modify the data that was
stored in the shel, modify tn the temporary value you
assigneditta, then re-asign that temporary value backto
the she before closing agin
That's about allwe have space fortis tutorial, but keep
reading, because wel cscs one inal option for persistent
data: relational databases (or example, MySQL).
Coding Academy | 27Fundamentals
Lock down with
data encryption
Learn the principles behind encryption — we unpack the
details of how it prevents snoopers from reading your data
“The first rule of data
png rg ne lyse tthe
VN reece ncn
trytofecurdotatem then nit werormaly
yt wrt nao atta pars can ed
Moe tet encodes and ahs mate
sc bj dors Home
therere tres ver we vent ep our rato
suri Poura we mds tant tna an rac
net putt ona USB tht cods ext Water,
thecae wereoomae seta pos escort ee
wt is nd
fetus
erenyton
encryption is this: never to. ,.°%:"
try to create your own’ westadisy
Asymmetric et
rule of data eneryptin is never try to create your awn Unies,
that's. youhavea PRD inthe appropiate area of
‘mathematics and plenty of experience. There are number of
standars that are generally considered unbreakable, and
most languages havea good
set of encryption bras that suppor these. These libraries
willbe fr more secure than any youcan create youre
just make sure you read the dacumentation properly to
0d usinginsecure options. Here we're ging to break
this ul but onto show how encryption woe
‘encryption starts with data you vat to hie. then
applies some method of rendering that data unreadable.
deal, t should have some method ofrecovering te data
that only the orginal use ean perform, Usual thisis done
witha password (when the passwordisa string of binary
infoemation rather than an aipha-cureric word. refered
toasa’tey but thease principal isthe same). There are
‘wo ferent types of encryption: symmetric key and
asymmetric ey. The former uses the same key to decrypt
formation asi used to encrypt it while the ter uses
‘ho ferent keys,
Symmetric encryption
Here woe going a take aloo ata spl and fly
insecure) method of symmetric eneryption (380 known as
private ey encryption for text that ues the XOR exis
(OR) function. KOR takes two Binary gts a5 an input (which
caneach be ether Oo") toutpus if ene of the
snus a1. anda Oi ether nane or baths (See the XOR
“ath Table. rah).
We can XOR stings of data by XORe each tem in tun.
That simp XOR provides we ned or our eneryption
method which wel get onto in aminute Fist Welook at
the formation we'e gong to encrypt
“et ike all comptr data. stored in binary ass and
6s. SCI tot encoding stores each character a a sting of
ght ts of binary evormation For example. is
(01000010 is 1100101 ana is OLLI, ASC tet
then s just chan of thse charac
long." hereto is OL00001001100101010310.
Nout back to our encryption method. ee going tse a
password tha’ ust singe character (we told you it wasnt
song be secur.Rcanbe any snl character: Ardour
éenerytien methadisto XOR each tra cur ox vith our
key (See XORng characters box, above ih).
“That ciphertext i ur urveadable text Without knowing
thekay. theres no way program ean read it. ors there?
‘We knon the kay its), but wean we use this to ge
the orignal ext back? Actual realy simple Backin
‘Symmetric eneryion great or secur
fins but thas some protlems when securing
communion For tr
way of sharing th keys th evanjone. you wanted
Snencrypted comeancation th fr exo,
Google, you's somehow need tan 3 ey, and
thiskey would need tobe ciflerent fox evry person
Googe communicated ith otherwise they dbe
able eavesop.
Tot arr this. we have asymmetric
encryption (hich aleaknoum as public ey
tn) nhs method are are to keys.
28 | Coding Academy
onepriicand the athe: prste Anything that's |
ecrypted withthe pub ey can ony be decrypted
vith the pate ay and isa vers,
Now fr exam you need an enryated
cermunieaton ith Gece you ony need to
‘row Google's public kay. Using this publ kay
encryption, you canextange 9 hele ue ke or
a session of srmetric eneryptin
For urher nfo and an example ofan asymmetric
implementation, see Nel Bothcks itor
‘woratechradarcom/news/interet/data-prvacy-
how safe s-yourdat-in the cloud-1170332/1Lock down with data encryption
Dea ae Ue a eS)
Text
co
2 We can encrypt.a stream by XORing each character in tun with our Key
school you may havelearned that (a+b) +e=a+(b++e)
‘That with action a east, tdoesnt matter which order
youd the addition: you always gt the sare result. Welt
tums out that the same thngis true of XOR. But before we
can use that thre are two mere things we need to know
anything thats XOR wth ise, and anything XOR'S
with Ois unchanged,
‘So, key XOR key is O and text XOR O's text That means
that text XOR (key XOR key) is teat. and we ve just eared
that thsi the same as (text XOR hey) XOR Key. Soe our
cyphertext sust text XOR key we raw know that cyphertext
XOR keys our orignal text. Basicaly that's just a ely lrg
way of sayin that we can decrypt something exacty the
same way we encrypted
Statistical attacks
‘We naw have our methed of enryption and decryption but
It’snot very secure, Fist of al, there are only 256 possible
ays (ie include alight bt strings and not jst the ones
that have ASCII characters) t's perfectly feasible for an
attacker to check everyone tum. However it tums out
they don'thaveto,
In Engish text. some characters are very commen. and
thers are quite rare For example. the space character
usualy makes up 15-20% of allthe characters in apiece o
text andthe lowercase E about 1036 while lower-case Zan
bbeas ite 2s 0.02% and capital almost never
Since agven character nau text wil aways evaluate to
the same character in oureyphertext these proportion vill
‘come across. For example, given the cyphertext
‘00001008 00011110 00000011 01110001 00110010
(00111000 00100001 00111001 0110100 00100011
‘0010001001111101 01110001 01100001 o0100010
(01110001 00107000 00111110 00100100 0110001
‘00100010 001 10100 0011010001111301 0110001
(00110000 00100011 00110100001110001 00111110
(00111110 00100101 01110001 09100010 00110100
‘09110010 00100100 00700011 00110100
\We can see that the mast commen & bt strings: 01110001,
po ee Reel
° 0 o
° 1 1
r o 1
T 0
‘We can take a guess tha hiss the encrypted format
the space character
We know that text XOR tet is O and key XOR Os ky.
“Therefore we know that (text HOR key) XOR text is ey.
So.iveXOR the mast common character with space
(00100000), we get the key. nti case, its 01010001
\wbich corresponds tothe ASCII character'Q! Using this,
vwecan decrypt the whale txt to:
XOR ciphers, 3s you see, are not secure
“Ts is known as a statistical attack and is one of the
‘most common waysot attacking encryption. The simplest way
to prevent your dataaling victim otis to use an encryption
method that’ vellknown and been tested by
‘the best mind nthe business. AES, Two-fsh, Threessh
‘and Serpent are al good choices. As we mentioned athe stat,
{you should nd alibrary thatimplements one or more them
inyour language of choice,
You may have aticeda sight aw inthis plan, How do
yout outa server's pubic key nthe fst pace? Afterall
ifanattacker could trick you intousing their public ey. they
could read llth supposedly secure communications.
We get around this by using cetfleates When you install
web browser itll come wth pubic keys for anumber of
trusted certificate autherties, A webste can then get re
ofthese authorities to sig their public hey. When you go to
‘an encrypted webpage, the server sends youa certfcate
(hich contains both ther public key. and the signature from
the authority) and the encrypted methed ince you trust
‘the authonty you can now trust this oerticate and ead the
page sate inthe kncwedge that's been securely transrtted
‘and not tampered wth
“This method sit perfect First itrequres you totrusta
range authors (ou can see how mary authoies you
trast in your web browser For examen Feox, go to Edt >
Preferences > Advanced > View Certificates > Authortis),
Tes quite afew, and most ae probably companies youvenever
heardof, much ss ust I ahacker or disgruntled employee
getsint the computer systems at anyone af them they could
intercept almost ary encrypted web rat they wanted, But
‘that’s atthe only way these oertificates canbe subverted fan
attacker can find way of forging the ctl signature they can
trick computer into thinking a communication comes roma
particular source wen it doesn't in fat it was ths method
‘that alowed the Fame malware to getint raian computes.
Mrosoft had usethe insecure MDS (rather than the mare
secure SHA) hash and attackers were able fuse this to make
the computer think something was signed when twast't. Be
coxfull
Coding Academy | 29eee ie
ACADEMYProjects
32 Python 3: How to get started
We're moving to Python 3! About time
36
40 FTP: Build a client & server
Create your own network code f
yster with scripts
imes, dates & numbers
sing of complex dates
52 NumPy & SciPy: For science!
Become a data scientist
Coding Academy | 31Projects
Python 3: How
to get started
Join us as we investigate what is probably one of the least loved and
disregarded sequels in the whole history of programming languages
back Deer 2008 Pt 30
Wires b irre ce
“ate wees ron bbs ea nee
aesilrousrgtferbernstpen tbortoeane
Python programmers and distribution maintainers are a
Dec tlre san avy rt ram
fo eompe roi ore ea ouppode sto)
Windovox acres frost Pro we
Caray thecdoftie ine sre antares
sro cores buntl2000 Fathermore
eit cf to apr on pro ory.
tmry mina eres havent ben gre he 3 esr,
tryna abe nts keds wna?
Eaton scala projet Naan Dg ~
data ton hop waster oes
noses kairgto naar
Unotinse, th r haepen an estyton ce
finda tre lune Pa a aon
Pyten 27s tomnrs corps wih 3x oar eoes
Fepablteconeupath caste lve oh ba
sal megerrareathcistec ong hee ants
cca rare agate Lint at cat alr,
sata bya Grover afechon re pat corte
tGyronocnde testy rear hyo
Menno tt nts ate Wepre
Ses start tht ight ave Ben out
—
prt Halo wor
Great“ dos varkinPthon3 lit you
tstpromse.) Treresonit dns was tht pit
Hrrvior2avastret wer Pen Spt
# hen and ancora whet cern aed wth
DrcklsReraber that ctr dt eed en
trong hee tt dot wc we cee
pit irora eld hncion ch nissipe rn
Ses esos Seas hat stngasto ost
irae cig
Inaserse youn eens nton Pyten2
Sey clitree ocse ns
bracts staal Tsao eth honour
onerecveshraharatcs when coming
sovclatertsomunr bangs oyna At
eral lun group beset oheptons
areolapartol te Pon canon (Specs
ioscan tba rue) costal
cmap cs aquuncaar ppt
32 | Coding Academy
ow more flexi for programmers ~as a bultn
function, canbe replace, which might be sell youre
into defying convention or making some kindof Unicode
etecting/deying wrapper function, Your frst Python
rogram should have been:
print (Hello wor)
which is perfect compatibie with Python 2 and 3. you were
‘fan of using a comma atthe end of your pint statements
(Cosuporess the newline character), then sad news: this no
longer works. Instead, we use the end parameter, which by
Cefauit is ane ine, Fr example
print (Allon end=**)
print Cone tine)
Print in Python 3
A siancant proportion of Python programs cau be mde
compat wih sty aren the pt yt tee
aremrary other a letra trys tht cu go wore
“understand tem we must ist bea a th wat aly
chargedin ton
Testo the word does’ speak English, Infact. stot
thawor dest evn use Latin charctr et eve those
regs that tnd to use diferent eof acest
decorate the charactors Asa rst bess he ASC
Standard, numerous ders and ncompatble character
tncodings have mand Each graphare (an abtracin cf
‘havi isso ocala aed ech capt
assigned byte encoding sometimes eral nthe pas
ityarwartedto share docurent wth org carats
> The Greek ryptos grapha, which translates as ‘hidden
writing followed by anew line using the correct scriptTrdtoaly tet was encoded in ASC hie
cach character encoded as ot codepoint
whit ees you 128 characters pay wth
Some of theve characters are visible tetype
codes (SCI ergnated nthe 19603). and once
we've counted the familar phanumerc
‘characters there i eaty much room let
Because we ike things tobe bytes, several
256 character entrstonsof the ASCI encore
emerged. The most notorious ofthese ISO
‘88591 sometimes called Latin Ths widely
used character st (and thereated
Windows 1252) contains almost althe accents
requre forthe Latin scripted languages 2 wel
Python 3: How to get started
The Unicode revolution
asthe characters usedin the romansation of
‘ter langusges Asa esa ay common in
the westor hemisphere, bul doesn rely solve
the problem eseuhere
“Te correct olution would be a tana
encoding (er maybe a couple of thar) tat
accounts for as many as posible ofthe st ot
Characters anyone on earth might conceivably
wht type Obvious his wilrequte many
‘more than 256 characters, so wellhave todo
‘ay with ane character encoding tone byte
(fence the vergence of cdepotnts analyte
encodings), but ts fora greater good
Ferturatey al the wrahre abulating and
‘other rigmaralchas ben don, awe have an
answer: Uncade Tis acount or ove
100000 characters, bdrectoral say ore
Feature forms and mare Cent thee sre two
encodings in use: UF which uses ane yt or
‘common characters (making tentrely
backwards compatble with ASCI),andunto
four tes forthe more cosmopeltan ones and
UTF16, which uses two tes some
characters and four bytes for ater. Uricade
ras been widely adopted both as astrage
encoding standard acer internally processing
{ests Themainraeon ctr of Python 3s that
is predecessor inet dothe late
it. then plan ASC would hep, You could use one of the
alternative encodings you knew the people you were
sharing it with could da the same, butn genera you needed
taturn toa word processor witha particular font which ast
‘moves the problem elsewhere. Thanifuly. we nowhave a
\idely adopted standard: Unicode (see The Unicode
Revoluton box, above) that covers al the bases, ands
backwards compatible with ASC and as far as codepoints
are concerned) ts Latin extension. We can even have
Unicode in our domain names, though internally these are
allstil encoded as ASCIl, via a system called Punyeode
Pythan 2s far rom devoid of Unicade support, but ts
handling ofits done fairly superficially (Unicode strings are
sneaky e-encaded behind the scenes) and some third
party modules stil went pay ricely wth
Stringsin Python 2 can be of type st (which handles
ASC fin, but wil behave unpredictably for codepoints
‘above 127 oF they canbe of type unicode. Strings of typestr
‘restored asbytes and, when printed toa terminal ce
comerted to whichever encoding your systomslocale
specied (trough the LANG and LC_* envionment
variables in Linux) For any modem dro thes s probably
UTF-8buti's defintely not somethingyou should take
for grate,
‘Theunicode type shouldbe used fr textualintercourse
finding the length of, sling or reversing a string, For
‘example the Unicode codepoint for the lmercase Greek
letter iis 030 in hexnotation So we can define a unicode
string from the Python console tke so, provided aur terminal
can handle Unicode output ands using a suitable font
> pi= uudseo”
> print)
>> typelo)
«type ‘unicode>
>> len
However we were to try this on a terminal without Unicode
support. things wal go wrong You can simulate such a
scenari by tating Python with
SLC-ALL=C python
Now hen you ty to print the lowercase character. you il
run ito a Unicode ncodeE:ror.Essontiay, Python trying
‘and faling to coerce this oan ASCI character (the ony type
supported by the primitive locale), Python 2 alsa tries to
> The PyStone benchmark wil ikely be slower in Python 3, but the same won't
be true forall code. Don't be a PyBk efusenk without fist trying your code
ete
ET
perform this coercion (regarcess of current lecale settings)
‘when printing tafe or apipe so dont use the unleode type
for these operations, instead use str.
The str type n Python 2's eal just aistof bytes
corresponding to haw the strings encededon the machine
This is what youshoud use if you're wring your strings to
{dsk o sending them over a network o toa pipe. Python 2
wily and convert stings of type unicode to ASCII (ts enaabais
defautencocnginthesestiatons whicncoudresutin aan
tears Sowecanaogetafunepicharacerby sings Baan
UTP bterepresentaten det There arrestor qlee
comeing UncedecocepontstOUTFS(erUTFi6)btes, imam
butts to imply accept tha the character comenee
encodes tothetwobytes CFBDINUTFS Wecanescope mz)
these with an Wxnotation in oxdertomake Python
Understand bytes:
>> step = "XCF x80"
>> typetstep)
‘type'str’>
>> lente)
2
So m apparently now has two letter. The point is your
Python 2 code s doing stuf with Unicode characters, you
need tohave al kinds of wrappers ad checks in place to
take account ofthe localisation of whatever machine may en
it Youll aso have tohandle your own convesionsbetween
Coding Academy | 33Aleph, beth,
simel..The
2t03 program
Python 2 code for
Python 3. Shalom
str and unicode types, and use the codecs module to change
encodings as required. Alo i you have Unicode strings ir
uF cade, youl nee to ad the appropiate declaration at
ne top of your code
#tcoding:utt8-*
“The main driving force for anew Python version was the need
to rewnte rom the gr
strings and thee representations to spy ths process,
‘Some argu that fas miserably (see Armin Ronacher's
rant at http//bitly/UnicodelnPython3), burly
‘cepends on your purposes. Python 3 does away wth the od
Unicode type ently s
1 3is naw
se
nee everything abou
Unicode-rientate, The str typenow stores Uni
codepoints. the language's default encoding is U
eed forthe eadng decorator above) andthe ne bytes
ject stores byte arrays ke the old str ype. Thenew str
ype wil ned to be converted to bytes ifit'sto be used for
ary kind of le /O.but this tra va the straneoded)
‘method. yu reading Unicode tet es, youinged to
‘open them in binary "fb mode and comert the bytes toa
string using the converse bytes decodeQ) method (see
‘ordeals bottom lf Python 3 however brings
ares besides al this Unicode malarkey,
about many othe
some ofthese are just the removal of legacy competiity
code (Python 3.unlke 27 doesn't ned ta be cormpatible
with 20), some of them prove new features, and some of
them force programmers to do things aferenty
Butwat there's more: fr example functions can now be
passed keyword only arguments even f they use the fangs
syntax to take variable length argument Ist, and catching
able ow requres the as keyword, Also,
you can nolonger use the uely <> comparison operator to
test or nequalty ~ instead use the much more styish =
which s aso avaiable in Python 2,
Automating conversion with 2to3
For reasonably smal projects, and these that aren't gongto
encounter any orgn characters ly that your th
2 cade can be automaticaly made Python 3. compatible using
the 203 too. Thishelptl chap runs predefined set offers
‘Shall Not
nni@jbmachine
Par) fi
Skipping
Est str)
Skipping implicit
ipping implicit
Precet)
(original)
teas
implicit
PL yetred
41,3 @@
POR
Ce
aCe err ies
SerrOr ieee ns)
Tur oreo es
tc
cert
pete
cote
Caer
PCH
Peet
Porras
(ne changein Python 3that has the potenti! to fs, detuning a it. ths in th behaviour ofthe dase dvson operator as
fummoris thatthe behaviour ofthe disin operator lands for integer cusn In we nares both nurertor and denominat
operator /scterent Fereample,ere'san second example the numerator isaflatand arent, then anints returned lone dies
fcerpt toma Python 2 seen trecperator ats a fasting pont the other otherwise afloat isretured th the
a2 urnnguhat yousinuatvely expecta of crret deem remainder If test one ot
1 thre tobe, The third ne uses the erp oor arguments 3 Het (r compen, then he
a2 cision operator 7 which wl return the behavoursunchanges
15 unde oom resus fat x ann “This means the / operator snow as close to
342 cepening onthe arguments. The ater rmathemateal vison proper ase can hope,
10 operators actualy backoertedto27.s0i% andissuesrwohing unexpected its wil
“Trisshows the rst command operat
34 | Coding Academy‘on yur Python 2 code, wit the goal of emitting bons ide
Python 3 cade, Using it can be as simple a:
$ 208 mypy2code.py
This will output cit of any changes agains the origina le,
‘You can aso use the -w option to averwite your fle dont
ory till acked up frst Some fixers willl run if you
specty them expt using the option. An example is
buffer wrich wil replace al buffer types with memoryvews.
‘These two arerit entirely compatible, so same tweaking might
necessary inorder to successfully migrate, Using
whole heap of effort, because searching and
replacing pln statements manualy fr stan
crudgerou task. The picture
hows the changes toa simple thre tne program
=the uni function isnow eed, because Unicode s
row impli and the print ine reworked, eve fuses
‘to format placeholders
Parlez-vous Python Trois?
wahoth yon 2rd Python's Why shed aly ny
Carpet one pele Ptr ven ths mc our er
re poring an tein or code mig
cures an sans hv dy beenbasparte 27
sel hen
For exam, you can get the new-argled pent syria
sing the following:
>> rom _future_import pint function
This is used wit the fram inthis wo, and_future_coesatt
Python 3: How to get started
>The popular
matplotib
module has
been Python
$3compatie
Since v2, fora
your graphing
and plotting
requirements
behave tke a standard module — nse ompiler
‘recive which n this case provide the moder print syntax
\Wecan ikewise impor dvson to get the new syle dvson,
‘orunicode eras to make stings Unicode by deaut There
tacts
Is another madule,contusinly calle fut, which srt par
ofthe standard brary, but can hep ease transition issues.
When Python 30 was released twas general regarded
as being about 10% slower than its predecessor, This wes
ot surprising. because speed wa
‘ew version aed many special-ca
removed in order to clean up the code base. Now that were
Up to vecsion 35, 2018).
woul be rice performance had since been improved
Unfortunatly thishasn'tbeen the case. ich you can
ising the PyStone benchmark, We testedit (see
3 really a priory for the
plimsations were
which was released in Decerb
has now comeback ram the dead twice and hence
possesses supernatural powers far beyond what ts dusty
2.3GHe processor would suggest
ous machine inthe offie. which
{tdort be misled: PyStone tests various Python
Internals, of which your code may or may nat make extensive
Uses important to test your code in both versions to get a
more accuratepicture.Youcan always se Cython
(mantained at htp://eython.org to speed up code
thats amenable to C translation (loops. math, array
access) othe bottleneck module
‘Guid van Rossum the author of Python, says that
Python 27 willbe supperted unti’2020, but that's no
1se to postpone learning the new version pow. Python 3
adoptions increasing so you wer't be alone. Half the work
isretraning your fingers to add parentheses.
Coding Reademy | 35Projects
Python 3:
Using functions
Everything you need to know to start using functions and developing classes,
modules, packages and objects in Python 3
‘eucanasoce
‘aretelants
eyo tenabo
crates at
raat
‘orien
a,
is tutorial wilteach you about Python 3 functions,
asses, objects, maduls ad package. ts main
purpose sto cantyalthese terms by using smal
examples that iustrate how to create and use themal. anda
large part ofthe tutorials about namespaces, which are used
‘everywhere in Python 3
Functions do two very important tingsfirst they enable
sto reuse cede and therefore reduce code repetition. which,
inturn,alows us to write less code, Second, unetions offer
way to Solve a big pcogram by sohing smaller components of
that problem
‘The flowing Python code declares a function that is
named aFunction:
ef aFunction):
printHello)
> This is the output ofthe myFunctions.py Python 3 script. The ast statement
‘ofthe script causes the error message you see onthe screen because
‘timestring0 cannot use a string variable as an integer
36 | Coding Academy
[As youcan se, a function defnton begins with the de
keywor followed bythe function name, I'snot mandatory
foraPython function to retuzn something or have a number
of arguments Yeu can now use Function as flows:
Function) |
Despite the fat rin the same message al the time, the
previous function takes no input and returns nothing.
However, most ofthe me a function takes one oF more
parameters as input ad returns one or mare values.
Te next function, we've called timesSring, takes two
parameters an integer and a string. as input and
returns sting:
def timesstrngtimes, text:
sring=""
for xin ange (0, times)
string += text
tum string
You can use timesStrng 25 flows
myString= timesString’3,"Hi)
printtimesString2,"Hi™)
However cling timesString®) as timesstringHis Hi vill
‘cause an ertor message because the frst parameter cannot
bbe used as an integer inside the function
‘As you might be aware, when a function returns
‘something, ou shou take care oft and’ uit common
to sve the retun value ofa function using variable. The
proper way fra function toretucr a val is withthe return
eywor. t's very important to remember that Python has no
‘ules about the types of the arguments athe return value
of function. .e.they canbe whatever you want. However.
its easy to forget that when a function works with integer
‘ales, wil now work when youpass a string instead You
shouldbe very caret because improper argument types can
be the source of nasty bugs. Although isnot mandatory to
deciarethe types of the function arguments you are feeto
do so. (See the screenshot. eft for more information)
Now aquick mention of function declarations in Python 3.
What the det kayo does is create anew abject and assien
‘aname tot Sa det creates anew funtion abject and its
‘ames used for referencing it Now that you know the basics
offunctons, we can start aking about asses,
A Python class
Apyonclss aay ofsoupng tctons anvartes
Core aj orertd ened thon cas pros
tre roca fetresof obec orerted owrarnng sich