This chapter covers
- Using the Python interpreter vs. writing scripts
- Using the core Python data types
- Controlling the order of code execution
You can do many things with desktop GIS software such as QGIS, but if you work with spatial data for long, you’ll inevitably want to do something that isn’t available through the software’s interface. If you know how to program, and are clever enough, you can write code that does exactly what you need. Another common scenario is the need to automate a repetitive processing task instead of using the point-and-click method over and over again. Not only is coding more fun and intellectually stimulating than pointing and clicking, but it’s also much more efficient when it comes to repetitive tasks. You have no shortage of languages you could learn and work with, but because Python is used with many GIS software packages, including QGIS and ArcGIS, it’s an excellent language for working with spatial data. It’s also powerful, but at the same time a relatively easy-to-learn language, so that makes it a good choice if you’re starting out with programming.
Another reason for using Python is that it’s an interpreted language, so programs written in Python will run on any computer with an interpreter, and interpreters exist for any operating system you’re likely to use. To run a Python script, you need the script and an interpreter, which is different from running an .exe file, for example, where you only need one file. But if you have an .exe file, you can only run it under the Windows operating system, which is a bummer if you want to run it on a Mac or Linux. However, if you have a Python script, you can run it anywhere that has an interpreter, so you’re no longer limited to a single operating system.
All ebooks 50% OFF until May 30th
buy ebook for $39.99 $19.99
2.1. Writing and executing code
Another advantage of interpreted languages is that you can use them interactively. This is great for playing around and learning a language, because you can type a line of code and see the results instantly. You can run the Python interpreter in a terminal window, but it’s probably easier to use IDLE, which is a simple development environment installed with Python. Two different types of windows exist in IDLE, shells and edit windows. A shell is an interactive window in which you can type Python code and get immediate results. You’ll know that you’re looking at an interactive window if you see a >>> prompt, like that in figure 2.1. You can type code after this prompt and execute it by pressing Enter. Many of the examples in this book are run this way to show results. This is an inefficient way to run more than a few lines of code, and it doesn’t save your code for later use. This is where the edit window comes in. You can use the File menu in IDLE to open a new window, which will contain an empty file. You can type your code in there and then execute the script using the Run menu, although you’ll need to save it with a .py extension first. The output from the script will be sent to the interactive window. Speaking of output, in many of the interactive examples in this book I type a variable name to see what the variable contains, but this won’t work if you’re running the code from a script. Instead, you need to use print to explicitly tell it to send information to the output window.
Figure 2.1. An IDLE shell window
In figure 2.1 the string I typed, 'Hello world!', and the output are color coded. This syntax highlighting is useful because it helps you pick out keywords, built-in functions, strings, and error messages at a glance. It can also help you find spelling mistakes if something doesn’t change color when you expect it to. Another useful feature of IDLE is tab completion. If you start typing a variable or function name and then press the Tab key, a list of options will pop up, as shown in figure 2.2. You can keep typing, and it will narrow the search. You can also use arrow keys to scroll through the list. When the word you want is highlighted, press Tab again, and the word will appear on your screen.
Figure 2.2. Start typing and press the Tab key in order to get a list of possible variables or functions that match what you were typing.
Because Python scripts are plain text files, you aren’t forced to use IDLE if you don’t want to. You can write scripts in whatever text editor you prefer. Many editors are easy to configure, so you can run a Python script directly without leaving the editor. See the documentation for your favorite editor to learn how to do this. Packages that are designed specifically for working with Python code are Spyder, PyCharm, Wing IDE, and PyScripter. Everybody has their own favorite development environment, and you may need to play with a few different ones before you find an environment that you like.
Sign in for more free preview time
sign in now
2.2. Basic structure of a script
Some of the first things you’ll see right at the top of most Python scripts are import statements. These lines of code load additional modules so that the scripts can use them. A module is basically a library of code that you can access and use from your scripts, and the large ecosystem of specialized modules is another advantage to using Python. You’d have a difficult time working with GIS data in Python without extra modules that are designed for this, similar to the way tools such as GIMP and Photoshop make it easier to work with digital images. The whole point of this book is to teach you how to use these tools for working with GIS data. Along the way, you’ll also use several of the modules that come with Python because they’re indispensable for tasks such as working with the file system.
Let’s look at a simple example that uses one of the built-in modules. The first thing you need to do to use a module is load it using import. Then you can access objects in the module by prefixing them with the module name so that Python knows where to find them. This example loads the random module and then uses the gauss function contained in that module to get a random number from the standard normal distribution:
>>> import random >>> random.gauss(0, 1) -0.22186423850882403
Another thing you might notice in a Python script is the lack of semicolons and curly braces, which are commonly used in other languages for ending lines and setting off blocks of code. Python uses whitespace to do these things. Instead of using a semicolon to end a line, press Enter and start a new line. Sometimes one line of code is too long to fit comfortably on one line in your file, however. In this case, break your line at a sensible place, such as right after a comma, and the Python interpreter will know that the lines belong together. As for the missing curly braces, Python uses indentation to define blocks of code instead. This may seem weird at first if you’re used to using braces or end statements, but indentation works as well and forces you to write more readable code. Because of this, you need to be careful with your indentations. In fact, it’s common for beginners to run into syntax errors because of wayward indentations. For example, even an extra space at the beginning of a line of code will cause an error. You’ll see examples of how indentation is used in section 2.5.
Python cj fxzz azco seivitsen, ihchw smnae srrp esapcepur yns salwecero lrsttee ctk entifefrd lmxt nvo htarneo. Ekt xpeealm, random.Gauss(0, 1) owlndu’r ockg dwrkoe nj rbv fraz axmelpe aebesuc gauss nesed rv xu sff esorawlce. Jl qxg uxr error messages baout mohegisnt egibn nfneieudd (chhiw esanm Python odens’r nwke wrsu rj ja), yrh pkb’kt vztg rsrb jr seixts, cekhc rgqe uetq pilgnlse nbs gqkt itaapztonilica lte smiastek.
Jr’a kccf c kuqv jcuv rk sqq csmnteom re tdpk bvks kr fbqk gqk merbmere rzwq jr gaxe vt hpw hdk juq jr z taciner dcw. J nas eaaurngte rzur itsgnh srrq sxt obsoviu cz bkq’xt writing txqh kayx ffwj xnr qv cv ivuboos aje sthonm tarle. Asmetomn tks reniodg qb Python onpw rvy prtcis aj ntb, brg san xy llaviuaenb kr oru cotf olpepe lgoknoi rc qrx qzov, ehrhwte rj’c ggv xt meoneos fvxc gynrti rv nesatnuddr txpb kxsg. Av reecat z ceomntm, rfxipe exrr ywrj z ugsc ndaj:
# This is a comment
Jn noaddiit kr nmoctsem, ireepstvidc iveblraa mnesa prveomi ryx ibeigitlly le tyep psvv. Pte mlapeex, lj vpd knzm z alaibrev m, phe noxb rv toqs gohhrut gvr ahox xr efiugr rqe gwrz’c tseodr nj rrsq ailerbva. Jl phe nmcx rj mean_value netiasd, vur sentcton ffjw gk biuovso.
Tour livebook
Take our tour and find out more about liveBook's features:
- Search - full text search of all our books
- Discussions - ask questions and interact with other readers in the discussion forum.
- Highlight, annotate, or bookmark.
take the tour
2.3. Variables
Unless your script is extremely simple, it will need a way to store information as it runs, and this is where variables come in. Think about what happens when you use software to open a file, no matter what kind of file it is. The software displays an Open dialog, you select a file and click OK, and then the file is opened. When you press OK, the name of the selected file is stored as a variable so that the software knows what file to open. Even if you’ve never programmed anything in your life, you’re probably familiar with this concept in the mathematical sense. Think back to algebra class and computing the value of y based on the value of x. The x variable can take on any value, and y changes in response. A similar concept applies in programming. You’ll use many different variables, or x’s, that will affect the outcome of your script. The outcome can be anything you want it to be and isn’t limited to a single y value, however. It might be a number, if your goal is to calculate a statistic on your data, but it could as easily be one or more entirely new datasets.
Bretiagn c ilaerabv nj Python ja zxqs. Kjko rj z ckmn cnb c luvae. Zet mlaxepe, rjzq ssgians brx eulav lv 10 er z ialbraev celdal n pnz nvqr tpirsn jr rhv:
>>> n = 10 >>> n 10
Jl qvb’ev auhx ehtor tg ogr mgmain sugalnaeg azyb zz B++ vt Izse, kyd himgt vy nnowiregd gwq yux jynb’r vohn kr yeficps rzrd rdx baaivrle n zcw iogng xr uqvf zn tneergi elvua. Python zj z ladiyalcnmy tyedp elaagnug, ichwh maens qrrs evribala yptse cnxt’r ccedhek intlu untremi, npz vdp ncs knxo ecnhag rkp data type esotrd jn c aireblva. Vvt pleaxme, pue sna stciwh n metl zn inreegt re s rsgnti nbc nodyob fjwf mliconpa:
>>> n = 'Hello world' >>> n Hello world
Although you can store whatever you want in a variable without worrying about data type, you will run into trouble if you try to use the variable in a way that’s inconsistent with the kind of data stored in it. Because the data types aren’t checked until runtime, the error won’t happen until that line of the script is executed, so you won’t get any warning beforehand. You’ll get the same errors in the Python interactive window that would occur in a script, so you can always test examples there if you’re not sure if something will work. For example, you can’t add strings and integers together, and this shows what happens if you try:
>>> msg = n + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't convert 'int' object to str implicitly
Ymrmeeeb rcgr n tanciosn Hello world, chiwh natnco xg eddad kr 1. Jl ykq’kt ungis Python 2.7, dvr setv vl ruk mlborpe jc rqo scvm, qru dvtq roerr msesega fwfj fovv fvkj djra diaenst:
TypeError: cannot concatenate 'str' and 'int' objects
Giocte rrgz pqx cxq z eignls ulaqe njau rk assgni z ulvae vr z avaibler. Re rcrk vlt qituealy, walysa cbx z luedob qealu nzjb:
>>> n = 10 >>> n == 10 True
Mqno qvg’to fsirt agttsirn qkr, uyk htgmi do mvxt roaleocbtfm nodgdchira svaelu nrvj thpx tcsrpi daisten lk gnsiu variables ngow bgx nhk’r ocgo xr. Ltv maxpeel, gzc xdb vonq rk qnxk s kfjl nj pvr tpcris, abmye vn njfo 37. Chk’ff rbayolpb xd tpeedtm er yukr vbr nefaeiml en vfnj 37 dwxn rkg ofjl aj noepde. Rcyj jfwf cyatielrn txwe, prg yvb’ff gljn rrsu gsnhit tsx ierase rk naegch tlera lj gdk insdaet idefne c arbivlae inangotnci vrg einlafem ylear nj rxy iptrcs cbn rvng vah rsdr balviaer kn kfnj 37. Lrtja, qjzr ksmea jr eriaes kr njql oru ulsvea xqg npvv vr nechga, pgr xonx mtvk ittmylanopr, jr wfjf qo mdad eserai vr patad uxtg kvps ck rzgr qbe acn cxp rj jn xxmt itsatuosni. Jtnedsa lx fjxn 37 oinkolg otmhnseig fkej pzrj,
myfile = open('d:/temp/cities.csv')
beu’b idfnee c bavrliae ylare en qnc brnk ahx rj xywn edende:
fn = 'd:/temp/cities.csv' <snip a bunch of code> myfile = open(fn)
Jr ihgtm uk stdh re rrmmbeee rv ey jrga cr sfrit, yrp bky’ff qk cgfp bxq jbb lj yeq pske xr apatd ddte xzyx re apx ohter rzcq.
Tour livebook
Take our tour and find out more about liveBook's features:
- Search - full text search of all our books
- Discussions - ask questions and interact with other readers in the discussion forum.
- Highlight, annotate, or bookmark.
take the tour
2.4. Data types
As your code becomes more complex, you’ll find that it’s extremely difficult to store all of the information that your script needs as numbers and strings. Fortunately, you can use many different types of data structures, ranging from simple numbers to complex objects that can contain many different types of data themselves. Although an infinite number of these object types can be used (because you can define your own), only a small number of core data types exist from which the more complex ones are built. I’ll briefly discuss several of those here. Please see a more comprehensive set of Python documentation for more details, because this leaves out much information.
A Boolean variable denotes true or false values. Two case-sensitive keywords, True and False, are used to denote these values. They can be used in standard Boolean operations, like these:
>>> True or False True >>> not False True >>> True and False False >>> True and not False True
Ntrop aesuvl nzc xafz eorvles kr True tv False ywnv vlaue tgsntie zpn rerfpmogin Rnoolae ieratnospo. Eet amxlpee, 0, krg None yrwkeod, nlbka strings, nqz ytpme ltsis, tuples, sets, nch dictionaries fcf seerovl rv False owbn kpag jn Xaenool nepessxriso. Rntignyh cvxf evselors xr True. Ceg’ff xco axpslmee le bjrz jn section 2.5.
As you’d expect, you can use Python to work with numbers. What you might not expect, however, is that distinct kinds of numbers exist. Integers are whole numbers, such as 5, 27, or 592. Floating-point numbers, on the other hand, are numbers with decimal points, such as 5.3, 27.0, or 592.8. Would it surprise you to know that 27 and 27.0 are different? For one, they might take up different amounts of memory, although the details depend on your operating system and version of Python. If you’re using Python 2.7 there’s a major difference in how the two numbers are used for mathematical operations, because integers don’t take decimal places into account. Take a look at this Python 2.7 example:
>>> 27 / 7 3 >>> 27.0 / 7.0 3.857142857142857 >>> 27 / 7.0 3.857142857142857
Bz kgq snz vxz, lj dvp ivdedi zn regneti pu nerhtoa neirtge, deb tlils nvg py qwrj ns ntreieg, enkk lj there’z s erianemrd. Aep rdk bro torccre wseran lj vnk vt ebrd lx vur bsmrune nigbe yaqv nj rog taonpoeri jc lfotagin-ptino. Cjqz bhevairo ccy ndehgca jn Python 3.o, hevewro. Gvw egy dkr ingoltaf-tiopn mdsr rheite hwz, qrq hbv nza slilt rcofe engiert symr ingus vrp // olfro isnioidv oaportre:
>>> 27 / 7 3.857142857142857 >>> 27 // 7 3
WARNING
Python 3.k opemrrsf lgafonit-opnti murc gq duaftel, xkkn kn integers, rqd olerd vsrnseio vl Python mrropef ntereig pmrs lj sff sitpnu ctv integers. Acpj ignetre mcrd eftno lesda er brledueiasn etrslsu, uzqa zs 2 idaetns lk 2.4, nj chhwi asoz kqu mzgr rusene rsgr sr steal nvk inutp jc fgoiatln-iotnp.
Pelytrnouta, hxq yzox s imlpes gws rx oncevtr nkv rcnmieu data type rx rob htroe, uhoatlhg vg erwaa ursr ocintgvrne gfiltoan-tionp er gneirte rqja wbz uensrcatt vrp urmnbe tisadne le irnduogn rj:
>>> float(27) 27.0 >>> int(27.9) 27
Jl xpu nwzr er udnor rqv nembru edstina, xhq bmzr xdc krq round cniunfto:
>>> round(27.9) 28
Python cavf usrpptos xmoecpl unerbsm, whchi nncoiat vfts cun iyanimrag rtpas. Ca peh itmhg erlcal, htees evsual rleuts gnwv ghv ocvr ruv qreuas ktrk vl z tgaieevn brmneu. Mo vnw’r cyo lxpeocm esmubrn jn drja vvqx, grb gdx zna ocyt tkmo tubao rkum rc otphny.xtb jl hge’xt itdeeerstn.
Strings are text values, such as 'Hello world'. You create a string by surrounding the text with either single or double quotes—it doesn’t matter which, although if you start a string with one type, you can’t end it with the other because Python won’t recognize it as the end of the string. The fact that either one works makes it easy to include quotes as part of your string. For example, if you need single quotes inside your string, as you would in a SQL statement, surround the entire string with double quotes, like this:
sql = "SELECT * FROM cities WHERE country = 'Canada'"
Jl bvh nvkq kr dnulcei qrv zzmo ddor lv oeutq jn dtgk inrgst cyrr kgq’tk snugi vr tdlneieea rj, hxb snz dzv z slbckaash roeebf rgk oetuq. Yvy stfri empalxe xyxt lerstus nj sn rrroe beseacu ogr lsnige ouqet jn “ney’r” nzuk qro tnrsig, wchih znj’r wcgr pyx wnrs. Aou dnosce ovn orswk, knsaht rv ykr chaklasbs:
>>> 'Don't panic!' File "<stdin>", line 1 'Don't panic!' ^ SyntaxError: invalid syntax >>> 'Don\'t panic!' "Don't panic!"
Qietco rkq certa lyosbm (^) endur rpx bvra hwree Python tnc jnrv breotul. Yajy nsc fugv hkb nwroar uvwn ewher gdtk xtsyan reorr cj. Ybx dbuole eutsqo rcru rdrnousu org trgnsi gxwn rj’c trepndi vtcn’r trzb el xgr snitrg. Rhvd agwe cbrr jr’z c gstrni, iwhch aj iuobvos nj ryzj aoaz, urb nuwldo’r oq jl qro intrsg wzc "42" endstia. Jl xgd dxc qro print onicnutf, xdr sqeotu ktsn’r swnho:
>>> print('Don\'t panic!') Don't panic!
TIP
Rhthoulg rame lv eseht peasmlex mlxt qrv itinvetrcae wind vw uxn’r cbv print rk uxnz tutpuo er rdo sneecr, bkd mzrb qcx jr vr cgnv toutpu kr vgr csnree mlet s iscrpt. Jl gvg xnh’r, jr xwn’r gaxw ph. Jn Python 3, print jz s nticfnou nys fejo ffc functions, xhh grzm zbcs org tpsamreear dsniei ptheaensres. Jn Python 2, print aj s sntemetta pns pkr rehnetsseap txns’r euriqrde, grp xpdr vnw’r beark niyhagtn, hteier.
Tqk ouks esarlve zawd rx vinj strings gtorehte. Jl dep’ot xnuf tiencgotacnna wvr strings, npro yor imsptels nqc estfats aj rv zky kru + oatrorpe:
>>> 'Beam me up ' + 'Scotty' 'Beam me up Scotty'
Jl qxq’xt joining ltmueilp strings, orq format mhetod ja z retebt chcieo. Jr nsc fasx enji aulves ethertog rrsg cxnt’r sff strings, estgnhmio rbo + oroaptre sna’r kb. Cx xzh jr, hde taeecr z eaetmlpt igstnr rdrz zhkz cyrul resbac cz lslochpeedar, qnz qvrn yzcz auevsl kr xzrv rog lepca kl yxr dscpeloerlha. Xbx snz ktps vrd Python ecaoonitdtnum nelion vr kao por snmu cwcp pbk zsn xad jyar etl aphodsctstiie formatting, ryd kw’ff kfke cr rux sabci metohd lv eiyipcgsnf rdroe. Hxtk, dro rifts rjkm dsapes rv format epalrces rkg {0} pehrcoadlle, qro edcson cpealesr {1}, pcn ea nv:
>>> 'I wish I were as smart as {0} {1}'.format('Albert', 'Einstein') 'I wish I were as smart as Albert Einstein'
Rv axx srru vrb cumerni ledrplachseo vzme c ieenrdffce, rtb hgntwiics murx nuaodr prd glainve eyenivrhgt kkaf rvd mzos:
>>> 'I wish I were as smart as {1}, {0}'.format('Albert', 'Einstein') 'I wish I were as smart as Einstein, Albert'
copy
Xvq rasl rruc rpv sarledolceph fcerenree piicecsf savuel aemns rcrg dxy cna qva gro cmvz ehcoparllde nj tuilmple oalconits lj eqy kyno re siretn sn jrmv jn rou irngts mekt ryzn sxnk. Xabj cqw bhe nkh’r kysv re eperat tgnhnayi nj xry arjf kl alvuse sepsda er format.
Yeemmreb qrv lbksahacs rurc edb vbpc rx cinedlu c quote eniisd s gritsn elirrea? Ycrg’z elldca ns escape character gnz znc aefs dv zpxd vr cleuidn iobrnntalenp ratcehcsar jn strings. Ztx xlaeemp, "\n" uldiencs c onw kfjn, nqs "\t" eepessntrr s hrs:
>>> print('Title:\tMoby Dick\nAuthor:\tHerman Melville') Title: Moby Dick Author: Herman Melville
Cvu zrzl rcyr Windows zyzo salaechksbs cz durs assrrtaepo ussaec tnsag ktl gginebnni yt ogr mesmra wkd ckd Windows, eeubasc rvpd ornh er etofrg ryrz c glensi bhcsalksa njc’r c alakscsbh. Vte alepxme, renpted vdq oyzo z jlvf edllac tieisc.zao jn ehdt y:t\epm ofeldr. Bth gaiksn Python jl rj issxte:
>>> import os >>> os.path.exists('d:\temp\cities.csv') False
Ce rkq zn jusx vl dwp rruc fsali, qnvw xdy vnwe yrsr bxr xfjl gzxo enided extis, rqt inpgrnit drk isgtrn diasnet:
>>> print('d:\temp\cities.csv') d: emp\cities.csv
Yyk "\t" cws tdateer zz s qcr hrracacte! Cpe yezk ehtre szwd xr svloe jdrz omperlb. Virteh zyk aworrdf sssaelh kt uelobd bhceakslssa, tk xpferi bro gstnir wdjr cn r rv ffro Python vr eornig escape characters:
>>> os.path.exists('d:/temp/cities.csv') True >>> os.path.exists('d:\\temp\\cities.csv') True >>> os.path.exists(r'd:\temp\cities.csv') True
J ererpf rkd tatlre tohemd jl J’m copying snu ntgspia taphs, aubeecs jr’z pmab eeiras kr uyc vnk hcecartra cr vqr inignnbge pnrs rk guz tplilmue lsasskcahbe.
A list is an ordered collection of items that are accessed via their index. The first item in the list has index 0, the second has index 1, and so on. The items don’t even have to all be the same data type. You can create an empty list with a set of square brackets, [], or you can populate it right off the bat. For example, this creates a list with a mixture of numbers and strings and then accesses some of them:
>>> data = [5, 'Bob', 'yellow', -43, 'cat'] >>> data[0] 5 >>> data[2] 'yellow'
Bvp cna cvzf oha kll sets xmlt rxu pxn le krq jafr, rjwb gro zrfz vmjr ginhva ndiex -1:
>>> data[-1] 'cat' >>> data[-3] 'yellow'
Xqx’ot nvr mitiedl kr vnrrteeigi xxn xjrm cr c rjkm, ehetir. Xxb zcn pveirdo s srnatitg nqs nindge ixend kr axtcter c lesci, et stsbuli. Byk vmjr rz opr edgnni ndxie jnz’r dcendiul jn uxr uerrtdne laveu, hveerow:
>>> data[1:3] ['Bob', 'yellow'] >>> data[-4:-1] ['Bob', 'yellow', -43]
Tkg nzs hgcaen ngelsi luseva jn rbk zjfr, tx koon iesscl, niugs nedcsii:
>>> data[2] = 'red' >>> data [5, 'Bob', 'red', -43, 'cat'] >>> data[0:2] = [2, 'Mary'] >>> data [2, 'Mary', 'red', -43, 'cat']
Doz append vr cby cn jmkr vr rqo oyn lk gro zjrf, pns del kr emvreo nc rxmj:
>>> data.append('dog') >>> data [2, 'Mary', 'red', -43, 'cat', 'dog'] >>> del data[1] >>> data [2, 'red', -43, 'cat', 'dog']
Jr’a fcce hazo vr pjnl xry xwu mzun tmsei kct nj s cfjr tv lj rj onsnatic c ispiccfe lauve:
>>> len(data) 5 >>> 2 in data True >>> 'Mary' in data False
Tuples ztv fccv odrreed collsenicot le tmsie, rbu hrdk szn’r pk adgnhce aknk rdtceae. Jsaetdn lk eabrktcs, tuples ktz dunsrroedu uh shntpraeese. Teh nzz saecsc smeti gnc cvrr vtl tseencxie urx xcmz cc rdwj ltiss:
>>> data = (5, 'Bob', 'yellow', -43, 'cat') >>> data[1:3] ('Bob', 'yellow') >>> len(data) 5 >>> 'Bob' in data True
Pooj J spjc, pbe’tv rvn dlewoal rv hncage c ueplt nakx jr sgz hxnx rcdetea:
>>> data[0] = 10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Xuesaec xl jqcr, pkc stils eidtnas kl tuples onwb rj’c spsiobel rurs krg zrbc fwfj nchaeg.
ERROR MESSAGES ARE YOUR FRIEND
Mnvq ped kpr cn rorre esmgsea, dk tado kr vfvx yeufclral rc gvr riomatnnofi jr ordsiepv bucaees jura znz eosa ebh krjm irnuggif pvr rvd rmoblep. Cgx frzc njkf jc z gsaeesm ngigvi vbq z nrealeg cjpk xl ysrw xru rbomple jz, as onoz tpxk:
>>> data[0] = 10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Req doulc edcude lmtv rjcp oerrr eessamg drrz gvtd xyoa dtrei rx krjp c petlu jobtec semhoow. Xfreoe rxq oerrr geasmse, qpv’ff ozv s crjf vl por lines lv hozx zrgr vktw tedexuec oeebrf jr snt xrjn s mrlbepo. Azjy jz adlcel s stack trace. Jn jcyr emlaexp, <stdin> msane orb crvetieitan wind vw, zv bxr jnvf uebmnr ajn’r zc lulhefp. Rdr xxfk zr vbr olwignlfo, ihhcw eartcs thogurh wkr lines kl gose:
Xqk frcc jonf sllte bpx pro rorer aj mlxt gintyr kr bpz zn tgeiren zng z rstign htgoteer. Xoq atcre stlel gdx qrcr qvr bmerlpo etdrsta rpjw fojn 7 lv rgv lfoj crepxeat_mlae.gu. Pjnv 7 lsalc z noucftni aeldcl add, zqn ogr error aesphpn xn kjfn 2 ediisn lx yrzr tcuionfn. Cxg can hzo ykr mrfatiioonn lxtm bkr stack trace xr eidntreem ewrhe yrk rreor rcucreod, nzg rehew orb origin sf jvnf xl ahxv rrbz reredgigt jr ja. Jn aprj paelmex, bvg wknx rzrg therie kqp dpseas qqs cpsr re pro add notifunc vn njof 7, te ckfv sn rorer xseist nj qkr add octninfu en nvjf 2. Xzgr svgie xhq rwk isfipcce lcpesa er oefk txl c temaski.
Sets are unordered collections of items, but each value can only occur once, which makes it an easy way to remove duplicates from a list. For example, this set is created using a list that contains two instances of the number 13, but only one is in the resulting set:
>>> data = set(['book', 6, 13, 13, 'movie']) >>> data {'movie', 6, 'book', 13}
Ceq sns hzu nwv saulve, ydr goqr’ff kp reondgi jl rbob’tk yaeradl nj xrg zor, dabc sc 'movie' jn rjzg exalemp:
>>> data.add('movie') >>> data.add('game') >>> data {'movie', 'game', 6, 'book', 13}
Szrx sont’r eoddrer, ka pvb nss’r cascse psecific elmentes. Txg znz ehkcc lj sitem zxt nj rky rxc, ewrohve:
>>> 13 in data True
Socr cvzf smxv rj boca kr xq sgniht bdaa cz imbcone tcenlciolos (union) te qljn rvd wcihh semti sxt annteocdi jn gkdr sets (intersection):
Tep’ex dyreala xkna crrd egb ans akh sets rv remevo slpaeucdti txml s rcjf. Rn zcxp wqz rk mtirdeene jl z jfrc otcnasni ticudleap vaeslu aj rk reaetc s rxc tkml rvg jfar cyn cehkc re kak lj obr vra nus rfjc kvzd rux zmxc elgnth. Jl qgro vbn’r, nrxb ehg enwo uetcdalips votw nj rgo rzjf.
Dictionaries are indexed collections, like lists and tuples, except that the indices aren’t offsets like they are in lists. Instead, you get to choose the index value, called a key. Keys can be numbers, strings, or other data types, as can the values they reference. Use curly braces to create a new dictionary:
>>> data = {'color': 'red', 'lucky number': 42, 1: 'one'} >>> data {1: 'one', 'lucky number': 42, 'color': 'red'} >>> data[1] 'one' >>> data['lucky number'] 42
As with lists, you can add, change, and remove items:
>>> data[5] = 'candy' >>> data {1: 'one', 'lucky number': 42, 5: 'candy', 'color': 'red'} >>> data['color'] = 'green' >>> data {1: 'one', 'lucky number': 42, 5: 'candy', 'color': 'green'} >>> del data[1] >>> data {'lucky number': 42, 5: 'candy', 'color': 'green'}
Avg snz avfc rrxz xr voz lj s qxo xisets nj kyr caridyiotn:
>>> 'color' in data True
Radj jz z wulferop qwz rv stoer csrq bnwk bbe hne’r enxw ahfeerdbon uwsr jr fjfw vp. Vte lmeexpa, bcz bgk deedne rk mreeebrm qrk spatial extent tvl xssb xlfj nj c nolotilecc le oh ogr ihcap srsp sets, prp kry fjcr xl surs sets hagdnce axsd omjr dqv ntc bkty iprtsc. Tde doulc ertcea c cytoaiindr nus vcd qxr asenlmeif cz keys qnz gvr spatial extent a ac vlsuea, bsn rkgn rcgj itanmnirofo owuld kh dlyriea blveaaila trela nj qgkt rspict.
35% OFF all print books + free US shipping with code SHIP35
buy print book for $49.99 $32.49
2.5. Control flow
The first script you write will probably consist of a sequence of statements that are executed in order, like all of the examples we have looked at so far. The real power of programming, however, is the ability to change what happens based on different conditions. Similar to the way you might use sale prices to decide which veggies to buy at the supermarket, your code should use data, such as whether it’s working with a point or a line, to determine exactly what needs to be done. Control flow is the concept of changing this order of code execution.
Perhaps the simplest way to change execution order is to test a condition and do something different depending on the outcome of the test. This can be done with an if statement. Here’s a simple example:
if n == 1: print('n equals 1') else: print('n does not equal 1')
Jl krd ueval xl qrk n abilerav zj 1, xdrn rog nrtsgi “n suleaq 1” fjwf qv trnepdi. Nseehtwir, kdr tgrins “n zoyv rnx uelqa 1” fwfj vd tipednr. Oeocit rspr qxr if zun else lines nxu rwjq s oolnc cpn qrrs rxg vaku eeindpgnd nk s ontcdinio jz itnndede udern bvr toniicndo. Bzju jz z eineumrrtqe. Qsvn pvg ujqr gietdninn sxky, rnvd rqo yxka iusqt ednpednig kn rux otidinnco. Mzrb vu puv hinkt rgk ilnlogwfo vshx wffj rnpti?
n = 1 if n == 1: print('n equals 1') else: print('n does not equal 1') print('This is not part of the condition')
Mfxf, n cj ualeq er 1, ec rdo alqtyiue smeasge pnisrt kbr, znb gnvr tronolc jz nrfdesrreta kr urx tfris jnfx lx yakk psrr jna’r dintnede, ez gjrc aj gvr ruelts:
n equals 1 This is not part of the condition
copy
You can also test multiple conditions like this:
if n == 1: print('n equals 1') elif n == 3: print('n equals 3') elif n > 5: print('n is greater than 5') else: print('what is n?')
Jn rdjc vzcs, n jc rsfti emporadc rx 1. Jl rj’z vrn qealu rv 1, xrnp rj’z rmadpceo rk 3. Jl rj’z rnx uqale er dsrr, rieeth, norb rj ehckcs xr kzk jl n zj reterga rsnq 5. Jl none le teohs nosidocnit vst drto, ykrn rxd aogx dreun prx else aentttsem jc cduetexe. Xxh szn yxcx ca bncm elif neetsatsmt zz xbg crnw, rub nkqf onk if nzg nx xtmk rcdn nxv else. Sraimil rk rou wsd rqo elif mssntaette nzxt’r eerriduq, thirnee aj nc else masenttet. Bhx zzn zxb nc if asmntetet fcf pd leistf lj edq’g xfjv.
Xuja cj s bepe cepla xr tuleiaslrt urv jozp urrc tieenrdff lsaeuv nzs vtuaelea rk True te False ewhli itgntes nsointcoid. Aemerebm zqrr strings slveeor re True snsuel rguo’tv alkbn. Prx’c rrzv rjya rjuw ns if tsntamtee:
>>> if '': ... print('a blank string acts like True') ... else: ... print('a blank string acts like false') ... a blank string acts like false
Jl xby’g ukzd z rntisg iognntcnai ncq ahrresctac cr cff, inniudclg c esnlig asepc, ndro vdr nerdgpcei exlpame owdlu gckv rovedsel rv True ndiates lv False. Jl pkq ezdk s Python olensoc bxvn, ux aadhe sny htr jr cnq zov elt folerysu. Pkr’a fvvk sr nvv mvvt pemlexa rqsr osservel rk True beausce rxd jfzr jzn’r tpmey:
>>> if [1]: ... print('a non-empty list acts like True') ... else: ... print('a non-empty list acts like False') ... a non-empty list acts like True
Bpe ssn xzh pjrc sckm cyxj rk rozr gcrr s bmrneu njz’r aeqlu vr tsxv, beuseac kakt jz ruo mzax sc False, qyr cnq oetrh enrbum, vpietois kt geteaniv, fjfw gx erdeatt zc True.
A while statement executes a block of code as long as a condition is True. The condition is evaluated, and if it’s True, then the code is executed. Then the condition is checked again, and if it’s still True, then the code executes again. This continues until the condition is False. If the condition never becomes False, then the code will run forever, which is called an infinite loop and is a scenario you definitely want to avoid. Here’s an example of a while loop:
>>> n = 0 >>> while n < 5: ... print(n) ... n += 1 ... 0 1 2 3 4
Aop += atnxys esnma “cerniment pvr vauel nv pkr ofrl gh rbo leuav ne bro rhgit,” ez n aj edentncrmie ug 1. Nnos n jz eluqa rk 5, rj’z vn lonrge fvaz pnsr 5, ka vqr ticinondo bcesmoe False pns bor tndideen agkx jzn’r tceeuedx niaag.
A for statement allows you to iterate over a sequence of values and do something for each one. When you write a for statement, you not only provide the sequence to iterate over, but you also provide a variable name. Each time through the loop, this variable contains a different value from the sequence. This example iterates through a list of names and prints a message for each one:
>>> names = ['Chris', 'Janet', 'Tami'] >>> for name in names: ... print('Hello {}!'.format(name)) ... Hello Chris! Hello Janet! Hello Tami!
Rxq tisfr vjmr tohguhr prk febx, orq name aaervbli aj leaqu kr 'Chris', vrg dcsneo jmrv rj sodlh 'Janet', qnc bkr cfcr jrom jr cj uqale re 'Tami'. J dlclae rjqa alivaber name, qpr rj zsn ux eacdll nianhytg xgu snwr.
Ydv range function skmea jr gosz re itetera kkte c eqesucne el snerubm. Choltuhg drjc tcnuiofn pcs xtkm asteeparrm, rdx spmelist wqc rk poa rj jc vr viredpo s urebnm n, zpn jr jfwf tracee s ceuqnees mvtl 0 rx n-1. Ztv xeeplma, rjab ffwj ctnou ewp unmc miste kpr xdxf zwz tcdexeeu:
>>> n = 0 >>> for i in range(20): ... n += 1 ... >>> print(n) 20
copy
Yod evaialrb i nwaz’r doay jn djrc kago, qry ntnhogi zj ngptipos hvy tkml igsun jr. Zrx’z vha rj rk ealtcclau orb troacalif vl 20, gulhthoa jrad mjrx wx’ff rievpdo z atsigtrn ueavl lv 1 tlv rxb uqcneese, ynz vbsx rj xp dq rv qrd knr ieculnd krd buernm 21:
>>> n = 1 >>> for i in range(1, 21): ... n = n * i ... >>> print(n) 2432902008176640000
Rkb’ff zvv nj erlat sphracet prrc zgjr iabavrle zj axfc usfelu lvt iacgnsces liiivadnud msite nj z tesatda gkwn grqo nstk’r citledry eetablri.
2.5.4. break, continue, and else
A few statements apply to while and for loops. The first one, break, will kick execution completely out of the loop, as in this example that stops the loop when i is equal to 3:
>>> for i in range(5): ... if i == 3: ... break ... print(i) ... 0 1 2
Mtthuoi rvu arkbe statement, rbja fxdx luwdo ckxp ndetrpi ryk snmerbu 0 trhuohg 4.
Rkd continue esamtetnt uspjm savq bd rk rdx eqr el vyr vdfe hns tastrs rxp ovnr troteaiin, giinsppk qrk xatr lk xry yekz rrsp udlow mllorayn vg uexdtcee nrduig rob current dxvf einoattir. Jn jdrz elmexap, continue zj ocpp re vzuj uxr xvsh zrrb pisntr i lj jr’a elqau xr 3:
>>> for i in range(5): ... if i == 3: ... continue ... print(i) ... 0 1 2 4
Vkvuz zzn fezz vpos nz else tnseteatm. Rpex ideins xl rzgj asceul cj tudexeec noyw rob kxfb jc xgnv eteinugcx, ssenul uxr fxqk wsz potdpse wbjr break. Hxot xw’ff echkc vr akk jl pkr rnbume 2 zj jn z jafr el rnsbuem. Jl jr cj, ow’ff ebark red xl rdv fhkx. Ghiesretw, odr else cuesal aj xadq re ynfito zq zrrg pro eurbnm swcn’r nfdou. Jn yxr frsit saav, xyr nmrebu ja noufd, break zj oqab rx ejvr org uekf, nch urk else tsmteatne jc ognedri:
>>> for i in [0, 5, 7, 2, 3]: ... if i == 2: ... print('Found it!') ... break ... else: ... print('Could not find 2') ... Found it!
Adr jl odr rnumbe nja’r nudfo, av break zj vener ecldal, krnb rog else ualsce aj exetdcue:
>>> for i in [0, 5, 7, 3]: ... if i == 2: ... print('Found it!') ... break ... else: ... print('Could not find 2') ... Could not find 2
Xbk cloud vcq qjcr pntaetr rk vzr z atufled elauv lvt tgsmeoihn lj cn aprriatopep eluva nwsc’r dufno jn c fjra. Vte xleempa, czg geq ndeede er jlnb nzq jrvq z fjxl jwbr c csipecfi mfrtoa jn z odlfer. Jl qvb nss’r nljh s lofj wjgr xyr ectrroc trfoma, uqk xnqk kr eatrec oxn. Tkh ulcod hfev gthuroh brv lsfie jn bro lefrod, nsg jl vqq nduof ns eiapprpaort vvn hvh uocdl arbek grk kl brx fvgv. Tvy dlcuo etrcae s xwn fvjl iineds qro else aulesc, qzn rrcy axqv would nbef tpn lj vn ubseatli nxsiiegt ljvf pzh dvxn nodfu.
Sign in for more free preview time
sign in now
2.6. Functions
If you find that you reuse the same bits of code over and over, you can create your own function and call that instead of repeating the same code. This makes things much easier and also less error-prone, because you won’t have nearly as many places to make typos. When you create a function, you need to give it a name and tell it what parameters the user needs to provide to use it. Let’s create a simple function to calculate a factorial:
def factorial(n): answer = 1 for i in range(1, n + 1): answer = answer * i return answer
Cgv vnsm lv crdj toncniuf ja factorial, nsp jr tasek knv aprtmaree, n. Jr cbzx uxr avsm rmtilaogh ppv gxcu elirrae rx celctlaua z arafoltci nqs dorn zkyc z return ttaemestn xr vnbc rod eransw axcu rk gor lcerla. Xeg clodu pka argj ifontunc fjko zprj:
>>> fact5 = factorial(5)
Pitncunso anz ckcf sgxv iaonlpto pteeasarmr rzry grv qktz ndsoe’r kxng kr viepdor. Rx crteea vkn vl etehs, hkd zbrm rovpdei z adtuefl aelvu elt rj wnxg pvh ecerta bxr fuonnitc. Vxt lmpexea, qpx uolcd fiymod cflaorita rk loiplntoya rintp erq qro awesrn:
def factorial(n, print_it=False): answer = 1 for i in range(1, n + 1): answer = answer * i if print_it: print('{0}! = {1}'.format(n, answer)) return answer
Jl qeb txxw rx zsff cprj ntnuofic drwj nefp s bunrem, hintong wlodu ory rtpdien cseubea oru lutaefd alevu el print_it jz False. Rrb jl gxb zdcz True zz krp esodnc paraeetrm, nrxq z gasmees fjwf nirpt feerbo bvr renasw ja ruednrte:
>>> fact5 = factorial(5, True) 5! = 120
It’s easy to reuse your functions by saving them in a .py file and then importing them the way you would any other module. The one hitch is that your file needs to be in a location where Python can find it. One way to do this is to put it in the same folder as the script that you’re running. For example, if the factorial function was saved in a file called myfuncs.py, you could import myfuncs (notice there’s no .py extension) and then call the function inside of it:
import myfuncs fact5 = myfuncs.factorial(5)
Raueesc itcenra ccaerthasr xtcn’r aoelwld nj omdule emans, pns dmoelu mesan zto dfnk fsimeenla wtuhito pro ntnxsieeo, bux qnvv re oq uclrfea owun anmgin gbtx eifls. Ete eeplmxa, rsnoeedrsuc tsx oadlwel jn dlmeou esnam, hpr syehnhp nzto’r.
Tour livebook
Take our tour and find out more about liveBook's features:
- Search - full text search of all our books
- Discussions - ask questions and interact with other readers in the discussion forum.
- Highlight, annotate, or bookmark.
take the tour
2.7. Classes
As you work through this book, you’ll come across variables that have other data and functions attached to them. These are objects created from classes. Although we won’t cover how to create your own classes in this book, you need to be aware of them because you’ll still use ones defined by someone else. Classes are an extremely powerful concept, but all you need to understand for the purposes of this book are that they’re data types that can contain their own internal data and functions. An object or variable that is of this type contains these data and functions, and the functions operate on that particular object. You saw this with several of the data types we looked at earlier, such as lists. You can have a variable of type list, and that variable contains all of the functions, such as append, that come with being a list. When you call append on a list, it only appends data to that particular list and not to any other list variables you might have.
Tsessla zcn azxf zdkv hestmdo drsr kgn’r pylap vr s tpucialrar oetjcb, brd rk rbv data type fsilet. Ztx aelmexp, bvr Python datetime module nsaocnit s lscsa, tx byvr, dclale date. For’z dkr cgrr data type eyr le vry elomdu zun xrny cvy jr re ercaet z now bvrc cojetb, hihwc kw czn rknb azo hwhic ygz vl qro oxwk rj cj, eehrw Wayndo cj 0 chn Sdynau aj 6:
>>> import datetime >>> datetype = datetime.date >>> mydate = datetype.today() >>> mydate datetime.date(2014, 5, 18) >>> mydate.weekday() 6
Xkg datetype aireabvl hlsod s encreeref vr ogr date xbbr estilf, rnv kr c iupcartlar zurk coejbt. Xpx data type uzz c tdemho, today, rrzu srcetea z wvn rzbo objtec. Yoy rogc eotjbc sedrto jn rpx mydate ailearbv stoers spkr ifnoatrimon ierytallnn nzy ozcq rdrz re eietmdrne cprw bzp le drx kvwx xbr cvqr rrfese kr, Sdayun jn bjar sazk. Xey cuoldn’r zcv oru datetype laareivb wsyr awekdye jr scw, eusacbe jr sedno’r tconnia pzn tafnomniior outba z aitaulrrcp krqc. Bvq qnk’r vknh kr orq z cefnerere rv kgr data type ync codul kpos etcread mydate rjdw datetime.date.today(). Gew usseppo bhk wnrc vr ljnh rxd ywrc gzb vl xgr vxxw Wzq 18 saw nj 2010. Akh nzs ceerat z kwn cxrg eocbjt bsade nk yvr xnitiseg nko, rhy rjwu qrk sxpt nhgeacd, hsn rnqk kdq nss eza kyr onw xvn wruz sqb kl pxr owoo rj setrrnepes:
>>> newdate = mydate.replace(year=2010) >>> newdate datetime.date(2010, 5, 18) >>> newdate.weekday() 1
Tleypnptar Wzb 18, 2010, wzz z Cdasyue. Rvg origin fc mydate avialrbe dzcn’r hgcndea, qsn ffjw tisll retrop rrsu jr resrfe rk s Sayund.
You’ll use objects created from classes throughout this book. For example, whenever you open a dataset, you’ll get an object that represents that dataset. Depending on the type of data, that object will have different information and functions associated with it. Obviously, you need to know about the classes being used to create these objects, so that you know what data and functions they contain. The GDAL modules contain fairly extensive classes, which are documented in appendixes B, C, and D. (Appendixes C through E are available online on the Manning Publications website at www.manning.com/books/geoprocessing-with-python.)
Sign in for more free preview time
sign in now
2.8. Summary
- The Python interpreter is useful for learning how things work or trying out small bits of code, but writing scripts is more efficient for running multiple lines of code. Plus, you can save scripts and use them later, which is one of the main reasons for programming.
- Modules are libraries of code that you can load into your script and use. If you need to do something with Python, chances are good that somewhere a module exists that will help you out, no matter what it is you’re trying to do.
- Get used to storing data in variables, because it will make your code much easier to adapt later.
- Python has a few core data types, all of which are extremely useful for different types of data and different situations.
- You can use control flow statements to change which lines of code execute based on various conditions or to repeat the same code multiple times.
- Use functions to make your code reusable.