Statistics Programming STATS 202A
Popular in Course
Popular in Statistics
This 34 page Class Notes was uploaded by Isobel Stanton on Friday September 4, 2015. The Class Notes belongs to STATS 202A at University of California - Los Angeles taught by Staff in Fall. Since its upload, it has received 98 views. For similar materials see /class/177956/stats-202a-university-of-california-los-angeles in Statistics at University of California - Los Angeles.
Reviews for Statistics Programming
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/04/15
Lecture 6 oat 7 2003 102052 PM senator obama 1s he ny world that can make them stop Oct 7 zoos 102123 PM Regardless of wha we owe McCaln for his servlce to the country the ru h 1s at he s too ol to have p Palln she would need t r n an assortment of eas 1n mll lon Sltuatlons ama seems to understand e ru o t of both economy and the wars we flght Oct 77 2003 102134 PM McCaln polnts his flnger at Obama obama polnts hls flnger at McCaln NO an NE 1s answerlng the questlons theyre very artful 1n dodglng lssues so nelther looks bad the debates ts are a complete and utter farce dlscouraglg to thlnk that these are our cholces for Presldent and Shell scripting was our rst foray into real programming there are some things worth noting The commands in this language express actions on les processes and other programs Aside from the operational aspects ofthis construction notice that it is relatively easy to go from an interactive session to writing code 39 nd we ll see this again and agaIn39 a In most cases the resulting script can be interpreted by any other Unix system Where are we We have had a look at the Unix filesystem and a basic command line interpreter or shell We have examined the basic directory structure ofthe lesystem discussed several kinds of les and seen how permissions work lquot We have introduced commands that help you navigate the lesystem cd pwd manage your corner of it Inkdir rmdir and nd les Fquot We presented some powerful tools for manipulating les and for working with text data mv c rm grep sort cut uniq and have worked with pipes to chain these commands together 1 Finally we examined how to create very simple programs from sequences ofUnix commands Specl cally the Boume Shell blnsh Today We will start to look at the structure of Python we ll talk about the basic organization ofthe language and get you started with some simple scripting tasks Our test case will be a pair of corpora related to the last presidential debate one coming from a live bulletin board hosted by Fox News and the otherfrom a live blog on the New York Times site A comment on the data for this class Our data so far have been log lesquot of various kinds we started with the access logs from ourwe server mov d to chat logs 39om IRC sessions and nally the output ofa standard email system In each case these data are generated automatically as part of some digital transaction and the transactions are best characterized as computermediated communications Far 39om being isolated examples this kind ofdata is becoming more and more prevalent an is not surprisingly the basis for research today s data fall into this category I i Elsezime iiniiir Candidates Tackle Economic Woes at E Second Presidential Debate debate John McCain tiled get ad lri rraiir arcrie adrdeariiri Ecanamit turmnll Tuesday at riisseeaiid Dresldentlal debate Willi Baratk Dhamai aririadrisirid a new dlari rd Prevent rareslasdres lav allawmd the daverririieiic to law dd riiartdades adulie debate Marcliirds of Wriisli rasdsed ml the eariciiide tn drive the eanipaiari dismurse iii a diresciaii that seems to be liming llie Rcudlzlicari numiriee in the W Bath andldatesi raeirid off in a lawn liallrszvle format iri NashvlllEi aesdsed each other DFCD LribullnE to H2 r ancal driresr udama rEDeatedlv panraved riis rival as a deviyulatm wlia lielded lettlle markets rdri Wild He bellevcs iri der gulatlan in every sirsdmsraiieeil Dhama said That s what we ve hem aaiiid tllraugll in The Caucus a Tim New Yum Times Poliliu mag Live Blog The Battle of Nashville ea iis s s LCmII and Uhamu pain a grant dcnlaftlm denim mlhm abnul rlie uieliailniilaiidrimrlimer Minaieianalimdvim i Mi lrmaloIorlu mnw snnniilim39 i N Ndfl vllm ml Nn39il illlime is m mum use dyn lu at um um I r i in s Singing ui untuzdidut hn crnmld lur lgnnnkanrunchmL mud m a damn imam bamh adiud Dumb ran ass ggd 111 in opponent is nu daylumnlnhen ii mes s ascn thLIssu The debates me 1 to 25 of B71 cummems 25m l39mhopiiiig LhalMcCain con nnnshisnegadvcanadltssinw i may seam m be doing nothing except mniving ulaaina highm W in she pulls M 5 the data we have mamadimmedalarmidealised dealt W39th 5 far have becauscofmcrgypolicy small muddaydskiugwumslm been artifacts of anuldgcuu Obnmayardsigu computermediated communications email chat web browsing are meersdy il sabig Inismkefarobamx mgnuegmimr aquot open to analysis whenyunlaywithlhedngsynugempwim eas 1m combinaan Dhmgaliwc ads and min d1 illolhzr Liesnakethehigli mad 7 dial39s whatAmuriua is looking or flu urship Today we ll look a bit at how we transition from the object on the right to ata se we can analyze in some sense 7 Jae 39 mam mpsay they39ll lake me gloves off Their gloves are i i round and theynecdmsdcklo39 sues Senator Ubama will is win this debate ande olecliml despite all haslmling the Republicans an so used to The data are encoded isfluerm here in HTML format lusncs Delayed ls allide nmlsd imzfm A mus aide Fashioned Yaybank W As one or the young people 39Im was terrorized by die gt ltspangt anaemeumassmsemumgtocnener 7 2qu western 1 Wspen a nxeze xcmencesnuszi t1t1e cment Yennahnx gtLnxltvltSpengt puaa amencennmx an massewmencecancenw 8217 ngpang that necaan Cantumes ms nagatlve attacks slnce they seem to be dolng notmng ce m 1 pmx ex pt mung Obama hlgher 111 me po 5 lt ltpgtI ac award Mocaln because of energy pansy euan me today ask szgn tually had a mend who was leaning ng where she could get an onama yard at ltpgtThe cm n ltpgtconverse You gs 1 anon of nagat e ads and Yahn and t for n 1y 1 up w v er ltpgt 54398217is a mg mlstaxe for Obama 0 gm negatlve mm why man lay mth the dogs th fleas lt pgt ltpgtEest take the high road mm mats azlhs what Marlee 5 Joanna for mm eadershpltpgt mnemmu Joeltctegt ltd1vgt lt0 01 indunzlinn nudem Our goal Forthe moment our goal is to extract information from the NYTimes and Fox News web pages this means we ave to identify how that information is described in code in the HT This task extracting data from web pages is also referred to as screen scraping the idea is that the data are not handed to us but we hav to sort it out from how the page is coded from the markup Markup In general markup or markup languages are a kind ofnotational device that describes how text is structured or how it should displayed it s fairto say that any electronic document you ll encounter consists of content and markup You see hints ofmarkup whenever the text in an electronic document as viewed in your browser or word processing program say changes color or size or font HTML or webquot pages TeX and Word 2007 documents are just a few examples where we nd powerful markup languagesquot Markup reader you are often most aware ofthe role that markup plays in formatting the fact that our NYTimes comment sections neatly indent the names ofthe contributors or that the comment list is shown in a boldfaced Arial font for example An overemphasis on the look ofa document might in fact make it hard to get atquot the underlying data being exhibited in a programmatic sense Markup The information provided by a markup language falls into two categories Structure and formatting Structure tells us how the document logically breaks down into parts paragraphs sections chapters and howt ese pa s are hierarchically organized from the document itself at the very top to the atoms of content at the very bottom Formatting in a broad sense governs presentation ofthe document which fonts to use to display it in what tone of voice to read it aloud how to break it into pages for printing and so onquot ht wwwwehreferencecum dlab bunks html 37111th Markup By focusing on comments 39om the NYTimes or FoxNews web sites 39 extract information 39om a web age that is not necessarily well captured by the expressiveness ofHTML Because the category ofa web page is fairly broad it would be possible to build the NYTimes comments sections 39om numbered lists from paragraphs 39om boldface and italic font speci cations and say nothing aboutwhere speci cally comments are 39 den We will come back to this topic in earnest when we get our hands on a serious parser that will let us examine the structure ofweb pages programmatically but let s just say that this task can be hard ag ass 515 E3 a I 5 Pi E39s Is 3 5 390 2 7 a 3 as E iszE eta EtEE sa a r 5 Some extra credit Kick the tires on this site and tell me what you think Article Discussion Edit History Piggy Bank Piggy Bank is a Firefox extension that turns your browser into a mashup platform by allowing you to extract data from different web sites and mix them together Piggy Bank also allows you to store this extracted 0 information locally for you to search later and to exchange at 4 thers need the collected information With 0 Download Piggy Bank Av ilable Screen Scrapers a Installation Instructions How to Write a Screen Scraper What39s New Screencasts l l Maw mm l l l WWW a s m a m a mecca rm gure 3 Extratung data 1mm the Vahoa White Pages Much information on the Web is formatted for human readers riot rnac ine lthimlgtMmezltlrgtA SmitkimWrsixbil Segundo we s program or agenl retrieve and lthln gtNamaltbgtAlrzd5iw ilhltEgtAddtesv ltbgt3ll w Fuiii AVE llSewldaliltlrgt manipulate Building Finder relies on Figured A mapping pauem in 11th wags Wrappers to prwide the nee ed data on a perrquery basis because the breadth and de in or queries prevents us rrorn storing or caching all data locally The obligatory step back Awk Awk was originally designed and implemented by its authors in 1977 in part as an experiment to see how the Unix tools grep and sed could be generalized to deal with numbers as well as text Aho Kernighan Weinberger sed awk and grep BTL 1977 What are you most proud of in the development of AWK AWK was developed by three people me Brian Kernighan and Peter Weinberger Peter Weinberger was interested in what Brian and I were doing right from the start We had created a grammatical specification for AWK but hadn39t yet created the full runtime environment Weinberger came along and said 39hey this looks like a language I could use myself39 and within a week he created a working run time for AWK This initial form of AWK was very useful for writing the data processing routines that we were all interested in but more importantly it provided an evolvable platform for the language One of the most interesting parts of this project for me was that I got to know how Kernighan and Weinberger thought about language design it was a really enlightening process With the flexible compiler construction tools we had at our disposal we very quickly evolved the language to adopt new useful syntactic and semantic constructs We spent a whole year intensely debating what constructs should and shouldn39t be in the language Language design is a very personal activity and each person brings to a language the classes of problems that they39d like to solve and the manner in which they39d like them to be solved I had a lot of fun creating AWK and working with Kernighan and Weinberger was one of the most stimulating experiences of my career I also learned I would not want to get into a programming contest with either of them however Their programming abilities are formidable Interestingly we did not intend the language to be used except by the three of us But very quickly we discovered lots of other people had the need for the routine kind of data processing that AWK was good for People didn39t want to write hundredline C programs to do data processing that could be done with a few lines of AWK so lots of people started using AWK For many years AWK was one of the most popular commands on UNIX and today even though a number of other similar languages have come on the scene AWK stiII ranks among the top 25 or 30 most popular programming languages in the world And it all began as a little exercise to create a utility that the three of us would find useful for our own use Can you provide Computerworld readers with a brief summary in your own words of AWK as a language AWK is a language for processing files of text A file is treated as a sequence of records and by default each line is a record Each line is broken up into a sequence of fields so we can think of the first word in a line as the first field the second word as the second field and so on An AWK program is of a sequence of patternaction statements AWK reads the input a line at a time Aline is scanned for each pattern in the program and for each pattern that matches the associated action is executed A simple example should make this clear Suppose we have a file in which each line is a name followed by a phone number Let39s say the file contains the line 39Naomi 123439 In the AWK program the first field is referred to as 1 the second field as 2 and so on Thus we can create an AWK program to retrieve Naomi39s phone number by simply writing 1 quotNaomiquot print 2 which means if the first field matches Naomi then print the second field Now you39re an AWK programmer If you typed that program into AWK and presented it with a file that had names and phone numbers that program then it would print 1234 as Naomi39s phone number Atypical AWK program would have several patternaction statements The patterns can be Boolean combinations of strings and numbers the actions can be statements in a Clike programming language AWK became popular since it was one of the standard programs that came with every UNIX system http W39WW techworld com auarticleZ 16 844 zprogramminglanguagesawk Would you do anything differently in the development of AWK looking back One of the things that I would have done differently is instituting rigorous testing as we started to develop the language We initially created AWK as a 39throwaway39 language so we didn39t do rigorous quality control as part of our initial implementation I mentioned to you earlier that there was a person who wrote a CAD system in AWK The reason he initially came to see me was to report a bug in the AWK compiler He was very testy with me saying I had wasted three weeks of his life as he had been looking for a bug in his own code only to discover that it was a bug in the AWK compiler I huddled with Brian Kernighan after this and we agreed we really need to do something differently in terms of quality control So we instituted a rigorous regression test for all of the features of AWK Any of the three of us who put in a new feature into the language from then on first had to write a test for the new feature I have been teaching the programming languages and compilers course at Columbia University for many several years The course has a semester long project in which students work in teams of four or five to design their own innovative little language and to make a compiler for it Students coming into the course have never looked inside a compiler before but in all the years I39ve been teaching this course never has a team failed to deliver a working compiler at the end of the course All of this is due to the experience I had in developing AWK with Kernighan and Weinberger In addition to learning the principles of language and compiler design the students learn good software engineering practices Rigorous testing is something students do from the start The students also learn the elements of project management teamwork and communication skills both oral and written So from that perspective AWK has significantly influenced how I teach programming languages and compilers and software development AWK and programming with text These tools let you program with text they are minilanguages well e not so mini that let you identify patterns in text les make programmatic edits and tie together many ofthe useful tools we ve shell seen in the Unix At very least your experience with the NYT data and the access log has probably le you wanting for this kind of ability rather than retrace a lot of old development effort we re going to take this need and jum two ahead a decade or Python Versions of Python can be downloaded forjust about any operating system you re likely to need There is also a large community of developers contributing code for all sorts of applications Wth a guiding principle like correctness and clarity before speedquot ow can we not consider this language a pgthon BOUT The Python Progrth GElliliEl Started Appiieatiuns 5 case Stan strong suppen for integratiar atieies eonies with extensive standar Website d ytho grant ii i gains and teel the language er quality more maintainable eei Python runs on windows Linux Handheids and Nokia mobile or DOWNLOAD Java and NET vinuai niaohines WNW Python is distributed under an A munt tron makes itlieeto use even forc gt7 The Python Sottware Foundatioi can Eymemm property rights behind Pythony u funds grants and other proyeets Read MmeDr tiy Python now 5 9 Developer uoiiate to the P r s Seclu ilyAdv story for re Hammers m Seounty advisory tor reort in Piioiisiiuil iiiii L US i Python 244 Release i Python 244 reiease candidai reiease of the oider Python 24 Published i i o nun Moving on We will now consider another scripting language that maintains the ease of use the fast transition to programming and portability but is more expressive Unlike shell scripts I expect that you will program in this language frequently both in and out of this class IE pgthon mom Gelliiiq Started Applications Sut e stu mumnon uiws con DEVELOF MENT QUle Lllks i gtgtlt Paeitage index Reporl website bug The Python Program Python is a dynaniie object can he used tor many kinds oi strong suppm for integration days Many Python pragram l gains and teel the language er quality more maintainaole eoi Python runs on windows Linux Hannh in Java and NET vinuai niaohines Python is distributed under an 0 makes it free to use even for c T e Python Sottwaie Foundatioi oiooeriy rights oehind Python u lunds grants and othei oroyeots i Read nioieor try Python now ottctt Source Developer Registrations are now open at 5th December Topics include HF Ptilziisiinii Tiiti Security Adm J y for re eourity advisory tor reorti in Puhiisiinu Tiiti i r i PLllJllSliL d Wch iirnizulia Getting started These lectures are meant to get you familiar with the look of Python and some of its basic constructions You should of course consult other documentation in this case the online tutorials and documentation are great http anw python orgdoc i 1 i Y 7 giiiniieyiiieevieuati m unwound nwumunthe tetanus I pullion ii tort it Pytltiiti nilt lilllt llltlliull python s qln39lda39u amumm39n im n GOC JME WMTON Dunninad Dwul39zii attz ii i itiaiiyianurstieatauno at Motivation ln lne eam ages enlymese vvlm penel el gleal Walln and seleclad Expats pessessee laelng ane nllllng smlls el lne aulllyle aeeulle lnem ll can be alglee mal llleaeyel me geneal pepulallen nnlle sllll nel lEIEFe 9e el nlln me lnvenllen el plnllng leennelegy nas been me ellne mesl Emanclpatnlytaces el meaem nlslely me nave enly laeenll y enleee me lnlemallen age and ll ls expelee lnal eempulel ane eemmumeallen leennelegy Will seen leuaee plnllng as me eemlnalll lelm ellnlelmallen elsllleullen laenneegy Amll nallel all usneuseneles alleaeyenn al last me pesenal eempule and ms numee ls sllll glen n neweve wlllle many peeple num zw use a eelllpulla lew el melll ale eelnpulla pluglzmllElSNDlI1ngllemES zlen l lealy BuprElE ln new mey DZ use mell eelnpulla mey ale eelllleu le usng applllzllens ln wa mal pluglzllllmls nave uenanllneu lel menl olle uesn lnee le he a lllslenalyle see me llnlllallens nele eee ln neme ane enlee syslmls glen elamaneallyln me eemlng yeals me musllealn nen le Ex se mls peglammaullly le usels ln a meamng nay all le mane ll easy lel ne Dgammels eennel all peglam ese eevees ln lnls Expeutturl lnle lne lulule ne nanl le Explae lne nellen lnal vlllually evelyeeey ean eaaln seme level m EDmpAtEl pugammlng smlls ln senee llsl as mey Earl lealn nen le leae ane nllle m le many enallenges lel pleglanlllng lalguages anu elllllenllllalls le lle useu by a mass auullalee ll eveyneuy ls a pleglanlnel pDDl pleglanllels wlll suley aleunu oeplng wllll mls sllllanen auegualely legulles a le llllklllg el me lllnualnlalla pleplanes el pleglanlllng languages alu uellel ll s Vel we nellelle malmele sneulu lle ne laea cul lsnncnun nelween leels useu hy plelessellals anu leels useu lel mucz mriusl as plelesslenal wllllas ll e sane lalguage anu alpnanel as men lea e s even me evel mele pevaslve use el eempulels ane sellnale ln evelyaspel elseelely ne Expatl me naee lel pugammlng smlls Will enlylnelease Wnle mesl anally smlnale Will be peaueee ey pmesslenals lnele Will be a neee lel mele peglammlng alle euslemlzaullly eyene usels httpwWPythonorgduceayCptehtm1 Our Vision conl allevvlng euslemlzallen lnmugn s lne ellell culld alse nave a male tglammlrlgwcullenlly mese gluups ale vastlyurldellepesm eulee eeee mudltlcanunswand pelsena zanens Will be lm elen elnn Wumm an i elllleslnleeempllel se ale vaupmm g nn e mass e leglsnes ellnlegyanng bug was llem meusanes el sellees ls alleaey a lelmlealle la cullmt smlnale e n seulee el emelvllse ls lee campiex le allen an e le e euslemlzallen vvlmeullllsllnvesnngaseleus ameunlelellellallellmelnle neeslallelng meyle uslng We ale lnleleslee ln enallges le me nnele smlnale devaupn nl as WElwln palneulal eevelepmenl leels u e II z m llhymmllng me pluglz lnzhllllyulzppllcznms hyzlwmy we wlll lelle eeenelllles el scale wlmeulsalallllang me ueslle elusels lel nlgnlypelsellalzleu sellw Appllcznms can he quotusva llee wlllleul el le el me senwale DI lnleiusllllese euules eleusl a ulllll l ll semng menlselves a llllelll mell peelsnleywlll lle allele aenlene mls I pleglalllnung lllelaeywe elnsell elng ellelyene e me salne ln m l ellllzalllllty planneu by me EwleEls usels el el leasens mese lneluue neeelll ele eels el iuslewlessng mell eleannl a lllley nave m ll mls ellell ls sueeesslul lnele eeule me man y mllllens evmttlall y ulllens el eempulel peglammes al lleus levels el pelleleney rne enaels mls Will nave en me slale el me all elsellvvale eevelepmenl ls nale le lmaglne rne nalule m sellnale Will enange le aeeemmeeale lne neees el mese pegmmmels ll pmtltul me sellnale peeess mal Will llx lnls lage enasle Our Vision II me llllll e we enlllslen mal eengulel pluglzllllllllg wlll ee laugnlln elemellayslaleellusl like leallln wllllng anu almnelle We leally meal eelnpulel pluglzmlllllg nulius1 eengulel use wlllen ls aleauy eelng laugnu The Lug peeel lel exampe nas s enn mal yeung enlleen ean tenell nem a eempullng emcanm Ol eellse mesl enlleen Wm l glen uptn ee smllee appleanm eevelepels lust as mesl paupe dm l tamme pelesslenal aulhulswtul leaelng ane wilting smlls ale eu le eve ne anese 0n DH vslm nlll ee metal pegammlngsmlls Fullhellme eelng nesel eul gaals a el less amelleus we leeus en leaenlng peglammlng le nlgn senee ane nuanS ma Dl eellege uneelglaeuales ll ne ale sueeesslul nele we expect mal me lenel glaees nlll seen lelen nllnln men llmltatlms rvle genelal eempunng llmds el pamculal lnleesl ale lne meve lenales lnlelmallen apalanees alle elvllan seelel lne eeeleaslng slze el eem mg ane me lneleaslng leaen el nelnelmng particularly nlleless nelnelmn make ll ssllle le snale lnlelmallen eelneen eevees ane le lnlelael mm mm ln le aellly le eenlle lnese eevees lmaglne lnal usels eeule make mal enn enanges le me sellnale emeeaeel ln say men GPSYEEEWEY el nanenee elgamzel lame man el ln aeellen lD eennleaelng u pgaees nem a venue el euyng canned adds en ape ea l as lnls neule gleally mpuvlel peeee le lmpeve lnen llle ey l nnal meynaee mem le ee 539 S a i E m eglammlng mell pelsenal leels le ee Exac y we ale successful nmrexpalts Will be age use mell eempulels ane emel lnlellgenl eevees muen mele elaellvel y leauelng mell level m nusnallen alle lneleaslng mell puducllvlly and Walk sallslaellen New lelsule pesslulllles Will uneeueeey e as well campule usels Will be alle le selve mel enn EDmpAtEl pellems mele mlen lemelng lne quot251m leenme su ppmquot Even ll meslusels ee nel peglam lagulally a lamlllallly nlm peglammlng ane lne slluelule m sellnale Will make lnem mele elleell ve usels el eempulels Fa examue nnen sememlng 932s nleng mey Will be age le mane a betla mmtal meeel ellne llneylallule nnlen Will allen lnem le llx el WUK aleune me eaem rney Will alse be age le assess ee l thm mey Earl mane e e anges ney Will quot251 lne selvees m an ExpEll rney Will be mele alle le eenvese nlm lng basic lllelaey ln aln aulemellve malnle allee yeu Knuw eneugn le enaen yeul ell alle aela a len eualls llneeessaly tut yeu alse Knuw mal yeu shuuldn l The Python interpreter I ii I executes them CTeall l lcqlll 30x24 nealub aaatsaa whlch python xasrhihpythah nealub aaatsau uythah Python 2 1 r25154863 Apr 15 2555 22 25 5 4 Apple Inc bulld 5455 ah aarhih i aapvriah credts ar llcense far hars ihrarhatiah gtgtgt priht Yhut ahs rhat ahs gtgtgt priht 31aa3iit m1 Z n Modules 5 a yum sawvy yr k code and then assemble it into les In Python les of commands are referred to as modules Programs are composed ofmodules 2 Modules contain statements 3 Statements contain expressions 4 Expressions create and process objects Program les You can also collect these statements into a le and execute it a Eux24 c nealub aaatsau uythah scrptpy rhat ahs m1 nealub aaatsaa nealub aaatsaa whlch python mh ealub aaatsaa is 7 scrptpy rrwxrrxrrx 1 aaatsaa wheel 55 net 13 11 57 scrlDLDY nealub aaatsaa nealub aaatsaa aat scrptpy aiasrhihpythah priht Yhut ahs priht 31aa3iit ealub aaatsaa nealub aaatsaa scrptpy rhat ahs 351 hsaiaa sastsaai n Modules We use the suf x py to indicate a Python module other les can make use of your code by importing you modu e au39 lcqin Eux24 c nealub aaatsaa aat scrptpy aiasrhihpythah a frst attshpt at a pythah program a try sahs prihtiha priht lhut ahs priht s1aasiit a assiah a yuruble x I ah hapiha that hacaih aahtihass his quotEqutve attacks nealub aaatsau uythah Python 251 r25154863 Apr 15 2555 22 25 5 451 A Dle Inc bulld 5455 ah aarhih ht i credts a D rype help i aapyria r llcense far hars ihrarhatiah gtgtgt impart script J Behind the scenes When you ask Python to execute a le it follows a few steps before it Modules starts working on your tasks en yo import a module as the last ourish Python executes code that s being loaded 2 Once byte compiled your code is then handed over to the Python Looked at more generally modules serve as libranes of tools we virtual machine WM the python runtime engine this is where make these available through the modules attributes your code is actually executed 39 gaunt if flle scrDLDYc nealab cocteuu cat cnptm m msrmm39py hon quota first attempt of n Man progmm a m some phntmqquot pm that an arm mnmm a Sign a vmiame I rim hoping mm mm continues 5 negative attacks Em mm nanth m mm 025154553 Apr 15 ms 2 57 six Apple Inc build 5425 on durum me my Lawright cmmts or lineme m Mimi mmmamn gt mm mm That one 391 ill scrwtgtlt l 39l hnmng um McCain continues his quotEun we an Behind the scenes When you ask Python to execute a le it follows a few steps before it starts working on your tasks ates your source code into t 1 Byte code compilation Python transl another format known as byte code a platform independen translation to byte code instructions Some other niceties rrwxrrxrrx scrlptDY scrp DY Mth its emphasis on code sharing you probably won t be surprised to fnd that Python makes some aspects of code creation sim le flle scrDLDYc For example ctieaumoqih 80x24 nealub cactcaa cat scriptpy aiasiaihpythah a first attempt at a uythah program a try sahc DrinLquot9 priht iihat ahci priht 3x15543xx4 a assiah a variable x i ah hapiha that hccaih cahtihacs his quotEqutive attacAs nealub cactcau uythah Python 251 r251i54a5a Apr 15 2aaa 22 c a ia 455 ah t a m 5 a thih Dre its llcense far more ihraihatiah m1 s haiplscnpti HelD ah haaaic scrlut mm script 5 a first attempt at a uythah praqr m nu ValumesRudusDutucacteuuscriutDY mu x I ah hapiha that hccaih cahtihacs his quotEqutive uttucks an acaaagiiaqih 7 50x27 nealub cactcaa cat scriptpy aiasiaihpythah a first attempt at a uythah program ah hapiha tha hccaih cahtihacs his quotEqutive attacks asticc Delayed is asticc achicai iihc far A Little DldrFushlaned Payback ccaih cahp say they ll taAc the gloves aft nealub cactcau uythah Python 251 r251i54a5a Apr 15 2aaa 2 25 55 4a1 Apple ihc build 5455 ah aaihih lype help copyright credits Dr llcense far more ihraihatiah sss impart script sss scriDtX I ah hapiha that hccaih cahtihacs his quotEqutive uttucks sss scrDtY Justice Delayed is asticc achicai iihc far A Little DldrFushlaned Payback Modules To be precise modules are reallyjust a bag of names a namespace the names within this bag are called attributes 39 39 39 z I U 5 variables or functions or classes we39ll de ne all this shortly oxaaaaiioqiii snxz i objects in a module the command from nealub cactcau Dyth 51 r251i54a5a Apr 15 2aaa 22 25 a Apple ihc build 5455 ah aaihih lD capyriaht credits Dr llcense far more ihraihatiah sss riah script mDarL xiv I ah hapiha that hccaih cahtihacs his quotEqutive attacks sss v Justice Delayed is asticc achicai rihc for A Little DldrFushlaned Payback sss import is sss resub haplng despulrlng x I ah desDulrlnq that hccaih cahtihacs his quotEqutive uttucks Modules III A I u architecture in Python Large import tools from other module les 39 39 i i In wulluel portability and code sharing Numbers 39 natinn point numbers complex numbers You can form expressions with standard operators like multiplication addition division remainder and oor division you can also include parentheses to ensure your rder computations are executed in the right 0 eneral converts all the operands up to the type ofthe most complicated operand in the expression The basics Next we39ll cover a few ofthe basic data types builtin object types in from which we extend therlanguage We will start with the following 1 Numbers 2 Strings 3 Files 4 Lists and dictionaries Numbers number literalsquot First Python how they are expressed as IE mi39Hl r 80x24 neale cacteuu mmquot mm 251 25154553 Apr 15 2am 225725 55 4 a 1 Apple Inc mum 5455 an durwtn h creams or itcense m more nfamutmn 3 5 35 a a mum pawt number 35 gt 351 a a complex quotmy 351 Numbers With this in hand we can use the interactive session as a big aum1 39 39 39 39 thn mindful ofthe numeric types involved mixed types covert upquot 555 eenxy 5e5155 5555e55 555555 555555 251 5251155553 155 15 2555 22 57 25 55 551 Apple 155 55115 5555 55 555515 rype help 1 555551555 155e515s1 55 license 55 555e 15555555155 555 3x15 555 135155 13555555555555551 555 55155 135155 a 55e 551551 s555e5e55 55555s 555 51515s L5 555 1315 a 13 555 15 55e 155e5e5s 1 1555 13515 a 135 1s 555 5 51555 555555 5111 convert 15 55 5 51555 13 1335 25 1531 15352 1 s 55 5555p 5 Numeric tools k quotquot I39I pow 55151 1 15 that contains a number of common mathematical functions 5e5155 5555e55 555555 555555 251 5251155553 155 15 2555 22 57 25 5 51 Apple 155 55115 5555 55 555515 5 5 555551555 155e515s1 55 license 55 555e 15555555155 555 155555 5555 555 5e1p5555 51 555e 1555 5s15 5555 55552 5e11 55 s 5 cash 5 deqrees 5 e exp v fubs 5 51555 5555 frexp v 55555 11 wu m55wmmu mmu mu ww5m mu m5sm5smv5 mu mmj 55gt 55555s155555s555515 5 9759951559257175 555 55155 55555s155555s555515 a 55515 55555155 5397599515592 5 n Numbers quot z 39 r r A r the type ofthe most complicated operand and then performs its math on the common type integers are simpler than oats for example and oats are simpler than complex numbers Operator precedence is settled as you would expect from any other language you een previously simple operators like and are evaluated rst from left to right with logical expressions coming last see Lutz for a complete list F nreredenre Numeric tools k 39 r powand 415 that contains a number of common mathematical functions xterm r p 55 55551e 5 no 5555 nu xt1555555555e5555sP555555555e55555e5s155s251155555552511555551555 5555s5 mmznn 555p5555555555555555555e5511555551ee55555551 mxmm r51s 55551e 1s 51555s 55511551e 15 Provides access 55 55e 5555e5551551 55555155s defined 55 55e 5 s5555555 Ins 5men ecasX 5e5555 55e 555 55s15e measured 15 555155s 55 x Ema 5mg Numbers This would all be nd if we wanted another calculator program 1 r quotquot make and assign variables A Mal XL 26 Mac 4 1 Apple Inc aana 5455 an durwtn wax way We gt z m a convert up to floats 13 a gt wueg we r taut 5 x 4159 a now usstqn a mama pawt value to x re a x is now a strmq Assignments explain what39s goingon below Ewmnll 80x26 nealub cacteuu Python Python 251 525154553 Apr 15 2aaa 22 57 25 55 4 1 Apple Inc butld 5455 an durwtn type help v cauYrtqht Breaths or ltcense for more nfarmattan a x Dman to an object representwq the value 3 a y and x now Dmnt to the some abject gtgtgty5 we create a new abject an nteqer 5 gt a by mung 1gtgtgt a and assacmte the name 5 wtth u we assacmte y wtth a new gt a abject anmaw leuqu x unchangedl J Assignments a W variables Python employs something called dynamic typing A variable is created when it is rst assigned a value by your code for example consider what happens when we type K 5 First Python creates an object to represent the value 3 the type ofthis object is 1nt just an entry in a search table a name Finally Python associates the name x the variable with the object that it refers to it adds a link to the object in the search table Variable names you there are a few rules Variable names can start with an underscore or a letter and can be followed by any number of letters digits or underscores D 39 3939 meanirm Finally you can39t choose one ofthe socalled reserved words names that mean other things in the language and de 2mm 555 whxle a 2115 51555 or bath 3ext 212 12 55 Xxeld break exc pt mam Pxxnt aaamaa 2mm 1 return 2 lambda m A step back Let39s revisit this list now that we39ve seen a bit more code Other tools Python actually has a fair bit of support for numerical calculations There is also a separate module 1 r access to the basic computations from linear algebra advanced random number capabilities and even the fast Fourier transform It will be interesting to compare Pythonsundry numerical extensions with R a language speci cally 1 l E Tl gmnr l cammwmn illlquotumuquotquot39 m Quinlanquot map 9 rmllms 5 chl s q Regr gyms Pymquot Limaq 6 Numeric and Mathematical M mmlnla llf tnh l ll39l llll tanner pnnnlt nllmrm nl rn nv sum nnl llllltlt39 mumrs in mm mlm n m mm Lllll llllll llllltllllih l gum lltll l llllltlllllh it l 4mg lulplamt mum ur wag ll ln Lllc psculll munn lllllnlk l u uurwnh luntmm a tnnm luncroala lllgllt39lanlllt39ltlllltlmlhdll m l nltll ll x mum ll l lllllllh smutle lk lall ts ls hilliiall limitms 1t w Fruit 5 4 2 s ltxlasilllg P HQT P IQ39 P 139Mlllli39v r N l 1 Programs are composed ofmodules 2 Modules contain statements 3 Statements contain expressions 4 Expressions create and process objects A step back Running backward through this list 4 We39ve seen expressions like mathsln3 0 or H1 therequot 3 We39ve also seen various kinds of statements including print statements and assignment statements 2 Assembling these statements into a le will provide us with a module 1And we39ve seen how programs can make use of other modules how they expose their data and functions Heavy li ing NumPy is an attempt to bring tools from what we classically refer to as scienti c computingquot to Python here are several such projects some falling under the general title of SciPy One interesting advance is socalled lethon offering a more powerful interactive Python shell As an upcoming homework you might get a chance to play with one or more ofthese a NumPy Numpy Array Interrace NumPy 0 Pamge The fundamental package need Down oad Numpv Python is called NumPy Tl lls pa a powerful Ndimensional a sopnlstlcated broadcasting l llnear algebra functio F Buy the Guide sophistica ed ran om nurn tools for lntegratlng Fortrar Besides it s obvious scientific use efficient multidimensional Comte also be used to replace Numarrz Numeric users should find the tr without some effa 39 with NumPv s Numeric compatib Users of numarrav can also trar Heavy li ing Sci Py NumPy is an attempt to bring tools classically refer to String literals rom what we as scienti c computingquot to Python l Scienti c Tools f SCiPy pronounced quotSigh Vie software for mathemancs In the shell we saw that there were three different ways to refer to a strings we could use single double or backquotes There are several such projects 39 some falling under the general title of SciPy aeaanrcnangas c i y wnion provides mnv i i kl i MM dimensionai array manipciar u JL 1 iiorary is built ro work with N prom smany userfriendlY could include one kind in a string delimited by the other One interesting advance is so 99 numericai roulI ES such as n called lpy hon offerquot a more Immutable Page numerical integration and on 39 9 ii i 39quotm Together they rut on aii oo powerful Interactive Python shell Hummus Symmsl39m Wm m mum I either variety quot qquot n M e Amquot 39 Emma may and 5 ay aquot that encode special byte sequences representing new lines tabs owermi enough to be deoei backspaces etc As an upcoming homework you afthe world s ieaoing SClEnkIS might get a chance to play with quotVW 39 t TET W B E 1 ompu eran spay or on one or more ofthese W SEW a W lSciPy 2007 Confe IPython Strings Heavy li ing 7 WW Frontpage Recall that in the shell single quotes were a kind ofjustthefacts Nump is an anem o brin 32312 th string no funny expansions in Pyt n can turn offthe escape mols from wha wezlassicalgy Bowman who an Enhanc escape the escape by preceding things with an r for raw refer to as scienti c computingquot IPython is a multiulatform Free m Py hm iuyrnoni An ennanoeo Python sneii A 93 qu 50 gtaraiiei Computing rnany ennanoemenrsoyer neale Cocteau pycnan gammy 2W controlling interactively aii Python 251 172212863 1 Apr 15 2mg 2325 There are several such projects Recentcnanoes 39 A WW to Wild Eusmmiz fiZiii Wi pylai a ltcense for more nfamutmn some falling under the general Sigfguage W mquot U m V y W y E y y r LL y a i F U i s x e i an aaina a c ain can nues n is neaa iye a ac s ney ine quotquot9 fs P 2 Asvstern for interactive oi gtgtgt IPvthon s new aeyeionrner gtgtgt X W Smquot Page I an naaina that nccain canunuesmhts nequuve uttucks immutable Page We also naye a more oeiaiiea as gtgtgt One Interestlng advance 395 5 inre reaiares in riie opening rei Emmyquot Lg zagcgg ngggfngggtedv WWW We called IPython offerlrig a more Arraonnenis current Version ma 1 reiease nis nequuve attacks Mom Animus powerful interactive Python shell Tne easiest wavto get your dues m L L L marina iies neasa note ear the j a quot X As 3quot Pcommg h meW rKr You the inordinate amount spam U s riie e newdututxt a turn arr the escape necnanisn might get a chance to play with We have maiiina lists for asers ar s prim riie one or more of mess rnirrors or these lists users ana r inewmamcxt in Maraquot have We mac D r manner 5w commits oasie sumrr Strings Finally we can have strings spill over to multiple lines either by using d endin y ing single or doublequotes an 9 each line with a quotquot or t triple yes triple quo es 15555115515515 80x23 555155 5555555 515555 515555 251 1251151553 151 15 2555 22 51 25 55 151 15515 155 55115 5155 55 551515 1155 5 555111555 151551551 51 111555551 r51 5515 15551555155 555 x 5 1555 b Operator overloading You can overload Python39s operators to deal with different types of objects operators like quot and for example have been extended to handle many ofthe builtin types We will see this kind ofthing in R Strings Operators like and have wellde ned meanings for strin the 111151111515139 N that we get an error ifwe try to mix types here 9 objects 111155115 80x25 555155 5555555 515555 515555 251 1251151553 151 15 2555 22 5125 55 151 15515 155 55115 5155 551515 5 1 555111555 151551551 51 111555551 r51 5515 15551555155 555 X115 15 1551115 5 15 z 5 15 115555555 5555 155555 5511 1555 5115 1555155 1155 1 15 51555155 115551151 555555 55555555555 15511 555 11551 5515555 I Strings Strings are just an ordered collection of characters and you can access portions ofthem via indexing and slicing 55555591115515 50x2 555155 5555555 515555 515555 251 1251151553 151 15 2555 22 5125 55 151 15515 155 55115 5155 55 551515 155 5 1 555111555 151551551 51 111555551 r51 5515 15551555155 1 55 555155 5555 1155515 Z 51111 1 555 xE 1155 5555 555151 555 x 5 r 1 11 55 1 555 453 1 55555115 1551555 5155 515555 r155 555 11555 J 11 55 555155 5555 5555151 5551 1 Strings Python has also been good enough to provide you with several functions to make dealing with strings a lime easier otteau lcqm 7 cm nealub cacte u Python Python 2 1 1251154153 Apr 15 2aaa 22 25 Apple 115 11115 5455 51 111111 Tvue he p 1 capyr1qnc 151e511s1 51 l1cense 151 more 11151111151 1 x 11 am 1511111 11111 McEmn gt xc Dtulze 11 am nap1nq 1115 155111 uDDer 11 1n HuPiNn 1n11 ncctxu1 1 15511101 3 gt xrEDluce hapmg reully 15111151 11 am 1ea111 nap1nq 1111 McEuin we M11111 1111 11 11 hap1nq 1 Hccu1n we 1 Methods This syntax is part of a larger circle of ideas that has guided Python39s development namely objectoriented programming We will take this up later 0511111511011111 83x27 115711111 55mm 1111151 111an 251 1251154153 1111 15 2am 225725 5 4a11pp1e 115 111115 5455 51 5111111 Woe help 1 5511111511 creams 51 lmense 151 more 111511111151 1 x e 11 am 1511111 11111 McEmn 1 W92 x 1 1112 11 1gt mu setutt 1 s on en 21 111 11e1555e1 endsw1th 1 expundtubs 1 n as 1sulnum 1 sulphu s 1 1511 1slawer 1 1ssDuce 1 1st1tle 1 1 1511 11111 lawer 1 111 3911p D rtitian 1 re luce 1 1111151 r1ndex 1 11111 rpurtitian 1 rsplit rstrlD 1 sp111 Splitl1nes 1 sturtswith 1 strlD 1 swupcuse 1 L1Lle 1 lemme 11 1111111 1 e 3 111111 1211111 1111 e 51 Strings Um what39s up with that syntax quot L L 39 39 quot39 39 39 thinghele In addition to operators like and as a type of object in Python trivial text processing tasks For the moment we will think of methods as a number of functions that names which happen to reference functions In Pvlhnn quot2 A r 39 r 39 39 39 lrin methods work only on strings we can use type and dlr to explore objects a little Online documentation 1 lt 1 1T1 3111111111111111111111 111111111 111115111111 11 11112121111 111 1 tra PymanL I n a euwcem e 111 sasenuencermesxm 1571 361 String Methods 111111 111 1111 111111111111111111111111 11115 111111111111 e1111111111111111511111 quotquot F11i1 15111111111 111111 1111111 11111111 11111111111111 11111111111 111 1111111111 1511111111511 1111111 15 11111 Once you know that string methods quotK exist there are a variety of online quotquot quot quotl quotquotquot l39 11 111111111111111111111111111111111111111 11111111 1111111 111111 1 resources to help you see what 5 11111111111 ai able 1111111 11111l1 5111111111111111111111111111111n11111111511111111111111111111g The easiest is just the online quot EI quot entation from 1111111111 111111111 n1 1 11 1 11 1111111111111 11 1 1 s python org 1111111 11151 w11111111111111111111111111 121111111111 11111111111111111111111 111 111 11111n11 11111111111111111111111111 1113 11155111521 m 1111mm 111 111111 11 5111111111111 11 1111111 L 5 1111111111121 15 11111111 1111111 1111111 11111111 511151411511111111 1111111 1111ll1 11111111 11 1111 111115 11 11111111111 111m 11 111 11111111111111 x1111111111 11 11111 111111111 1111 11111111111 1 111 1111111111n111 1111111 Strings trings are oftype str and you can You can also consult Python itse simply type help str 39 xterm auuimn3 sehcst1thsu r1x start end rgt haul with the sp ctr a s False otherwise st 5 aesthhths at that pastttah EDMPEFIHQ s at that pastttah Pie of strings ta try Return r e 1 rrtx mxth apt hal art te mxth optional ehc stDP scrrtx cah alsa he a tu gammAEnu Sexpandtahstahsxze egt string Return a envy of s where all tab characters are expahcec using spaces It tahsxze 1s hat Elven a tab stze of 8 characters 1s assumed amt sr1hcsca start ethJ egt 1ht Return the lamest theex 1h 5 where substrlng sub 1s cache such that sub 1s cahtathec wit UPtxanal art aha ehc a l l l l l l l l l l l l l l l l l l l l hth sstartend l re 1hterpretec as 1h slice notation l l Return 71 ch Failure l d Lists Lists are bui object types that represent collections of objects much ofthe syntax for working with lists is the same as that for strings but lists hold more general objects than characters Ordered collections of arbitrary objects N Indexed by position 3 Have variable length and can be nested Stores references to objects and not actual objects F Strings Finally to jump ahead a little Python also lets you iterate over strings and make simple kind of containment judgments chlr so 15 nealub cacteau uythah Python 2 1 r25154863 Apr 15 2aaa 22 25 55 48 A le xhc bul 5455 rhth uyrtah red 1 r llcense or more thrarhattah TYDE help r c th s a i feel a map cahtha ahl rlnl c a hattce that thls code is thaehtea more saah sss a th s rue see u e mhal 30x30 l healaa cacteat pytha pythah 251 r25154863 Apr 15 2aaa 22 5725 e1 tape the mm 5455 ah 1 cream a m ameh m YYDe HEE n r P n Dbumu asnmm u ham mhq gtgtgt Palln 1h x True i as z e xoy a operator avealaacthc hccath thalth ahaha latceh Bush r Eheney ass lehz i see x e rahaelt1egt a a cahvehtehce mx2mhaemeg gtgtgt I e xy laaa 2 la 1 2 a 1 s 5 7 a a Bush lcheheylj gtgtgt I e 12sslclthtahlleaae 1 l1 2 1 a lclthtah Bare gtgtgt I 2 l l 3 3 Lists so much more to say Well have a lot more to say about lists and how to operate on them in the next lecture but for nowwe have enough to be dangerous What I d like to do is get us back to the text processing application to do that we need to know howto get data into Python Files Here are some standard operations we would like to perform Notice the difference between read readline and readlines Technically Python will take care of closing the le for you when it s no longer needed how does it know that Again les are oftype file and you can ask Python for information also input open 39foxifinaltxt39 39r39 input open39fox7finaltxt39 default output open 39processedtxt39 39w39 s inputread read the file to a string s inputreadline read just o e line 1 inputreadlines read file into a list of strings outputwrites write string s to the file processedtxt outputwritelinesl write all the lines of l to file putputcipse manual close Files Ultimately as statisticians or students enrolled in a statistics class we are interested in working with data data that are often stored in les czteaut luqin 30x32 nealub eaeteaa neaa e1 rayrinaityt 1223434729a net 7 2aaa avi5aits pm McCain is aane far yne lifeela a up iiean families llke aurs are aaanaanina aur REDublicun rants tnen tnat says sane tnina Many in may party re npina snip Bur cauntr neeas resalutan ta issae s an n yants ta KEED tninas in tne gutter u nae y spaase y s an Efflce r in tne n iit y tnat s ya y a at vurlaus buses r na tne eaantry say st api i as up a n ai urpeisi n sinae nis pallcles are tne very anes tnat aa us in tnis ness ye re nat asnane n smp w i i t be ayina in ane af McEuin s wars eitne r yiiitaey families llke aurs naye suffered enauan ye yant enanae ye yant Dbu mm Pytha m eaeteau uytnan 5 1 25154553 A A pie ine aai nt an n 2 Dr 15 2am 2257 25 4 211 ia 5455 an durwm credits Dr license far mare nfamutmn e i reudlmeo e ei i 1223434729a net 7 2m meme pm nacain is aane far ynen hfeelang Repub iiean families llke aurs are aaanaanina aur Republican mats tnen tnat says sane tnin ny i snip r re i is e a a a M ina sni eea Dbumu su Dr i Am iaans nat tne tie es t Deaple llke Helium aaes Bur yias wan t ae ayina in ane af Helium s wars eitne yiiitary families llke aurs naye suffered enauan ye yant enanae ye yant Elba e Help mi tiaes rileiabie HieinaiiieL n Mn ne a1 i t u39 Lannnt be Metnads dev med nere rile Hi madnle Jiuiiuii xiein at iiiadeL bufferingll gt Me abject open a rile The We can he r y or a ran reading ldEfaulti wmtmg ni appending me the am te need it it caesii39t ms anen apenea sr uniting nr appenan t iiin tie ru e ea ynen ene tan iiHhrig Rdd a a to t mud an mnan F113 a a w ta the name ta 311 iiiaitaneaus ieading and will it tne nurseeing anganent is giaen a buter ed a a larger nynaers snecim ti i Rad a u ta made ta Wen tiie me nui ii a support any line enaina in the input rile yiiu a e n as WI Rlsm a me ea upenea aaiiis tne attritiite neulmes ue Fur tnis attnnute i a a n 39rn ar a up s ii e eentainina 39ililht ed with 39y m w nude l 39name39 gt del name Comments Let39s try reading in some data 39 ewnmei 50x23 39nealub cacteuu thn Python 25 1 r251 E e 54553 ADr 15 2aaa 22 5725 nu bul 5455 un durwln m t x uyrtun u s or license for more information e upenmumnuruwu eeuunneso s see lencamments 99 see i e camments see llawer 1223434729a act 7 2aaa a75a49 pm mcculn ts uune for wnen irreelunu repu hm u families like ours are ubundanlnq our rEDublicun rants then that says sun u r u Lt st peuple like mcculn uues our Kids won t ue uwtnu n une ur mcculn s wars elth er military runtltes like ours nuwe suffered enuuun we want enunue we want uu unu n Comments and here we explore how to strip out punctuation 39 at ml aaxro uuunu can t wln tr uuunu s us strung us we ve seen him lJ awer uuunu can t wln tr abumu s us strung us we ve seen him sss resub l abuma cunt wln tr uuunus us strung us wewe seen him uses resub s l abuma can t wln tr uuunu us strung us we ve seen him s resuu Auez 1 i abuma eun t wln tr uuunu s us strung us we we seen him Comments The module re can be used to match regular expressions make muml aux gtgtgt impart re see i e resub x7so gtl nl nu min is uune for wnen irreelunu Republican families like ours are abandoning our REDublicun rants then that says something nunw in our party are Jumping sn t r u ha i n the n ll ut rl uus uuses urau auntr says most peuple n the ntltturw are lean u Lawn s uuun not nu l t snq though since nts policies are the werw unes that uu us in unis ness e re not usnune at all u ju tnu sn use uunu s uppurcs working tnerteuns not the rtenesc peuple like Metalquot uues Bur Kids wun t ue uwtnu n une ur e uln s wars either ntltcurw families like ours nuwe s r fared enu u nu enunue we wun n McCain is uune for wnen irreelunu Republican families like ours are abandoning our REDublicun rants then that says something nunw in our party are Jumping sn List comprehen sions When you have lists of objects it is somewhat inevitable that we will need to loop over them applying some operation as we go V I that allow you to work on lists in one go they are a bit like the apply commands you will learn In R We39ll use them to skim out words that appear in a socalled stop list some sense gt m5 men 31 Danna ux r r qurnw 91a gt434 mu Elio Eania 7i s 333 and tu nllua an t 32 H gt5 ei 3n kalsaaunEganaaigsaegeia ii in an 39321 8 i 3 53 5 5 51in S 5n 3 2233 i an I53 5 near 47a any 3692 E n E 2 An End than 5 99x ca Suntan mini 5n ninnEn 3 l 55h 353 515 In HERE5 553 in 5 355 5255 nudu El M5 in math on up 0 w 1 K9 MRS17 Bangiu 33 5 hilt Comments the end ofthe stoplist words we read in evmmal 8b 29 l sss slap r DDen sLapLXL reudlnes m axh thutn y wusn h farn h wlthn y hatxh ahxh utn stap sstrp for s th slap a a llst aahprshshstah ap thut has far htch hac ah ut hi lxvl HEE ln is dune rar hhsh lferlanq Republlcun f mllles llke ours are aaahaahtha our REDubllcun rants then that says sahsththa Many h our party are Jumplng sh l l l Now we can get down to business layman r differences between the language used by each set of commenters y m 39ial 30x29 nealub cacteuu cut wardspy l mDarL re lnyt apsh Wtfnultxt reudlmeso hycaat apsh hycharascxc nealub cant nealub cant haplng that J u emu head 75 hycharascxc fi ka healah cacteuu n Comments In the stop list quot shhhai 33729 l sss wards s lSDlt l ss wards thaaath ts aahs far when h lfe h lanq v republcun y trahtttss llke aurs haa ubllcun h raats Lhen h th i ul th ur2 Jahptha sth aur a 25 ah he aah ta ha thy Spause v l aha aaraea tall aart a s peaple th the l htt thaaath nat h surpr tsth at ry ahss ha mmm Lu ml 1 that a bum s DDDr s warklnq h ahsrtaahs a L the rlche L peaple llKE y thaaath daes aur last want be laytha aha af haaath wurs ether y httttary iraht its llke y huve suffered enauqh we wunt y chuan y we wunt v sss ward s w for w th words if not w th cleanstap sss wards haaath republcun y trahtttss aurs ahahaahtha republcun y r ats suys v p rty needs h resalutan h lssues wunts Kee affcer y httttary warked vwmmw u m ma m W e ushume y Jahptha p i may i at h h J i want dy haaath wurs httttary rahtltss aurs suf fere chuVVEE abumu v 1 Assignment these Wordsquot really what were after Add substitutions and transformations to create real words change things How do we take the le of words that the program produces and compute word counts Today We will start with a brief overview of your projects where you re at and what kinds of help you might need Lecture 10 Where s in a name Then we will discuss namespaces and scoping rules for Python these really only become necessary because we will also have a look at functions Finally we will present a short teaser on object oriented programming in Python we will nish this in the rst half of the next lecture and which point we move to Functions Last time Last time we de ned constructions like list comprehensions We gave you a head start on your homework speci cally we that allow us to apply eXpress39ons over sequences looked at nested data structures like dictionaries of dictionaries We were somewhat content to apply regular expressions over lists of strings limiting the output to those that contain particular We also considered a number of efficient helper functions that patterns of chamders compute with lists and dictionaries By de ning functions we can extend the reach of these constructions in addition writing functions provide us with a simple mechanism to share computations or divide a larger task into smaller ones We closed by looking under the hood at assignments in Python Functmns Mm Ammmnsw m mm Oahued nymms mmkmwa H Wmmm yummmm 23mm M W We mm m mm mgnpmmsmmm giw wgg g quot6 msangbmm pmgm WWW n am mmwmw Wkwm Wm mu mm 5 WWW n x mp m ncesm bye 5 172 19ml 1731 m w Mmmmwm u xemxnimemmhandsan mm M W h mmmmmug m m mm an cad gum Fun mns I33 V1 u 5m van mmmmmaem mmmmm mm Wquot Pwvannms mm A mm an quotmm mmva mm van unmenhnkm sabyeum ii 391 wmernamesand mm isme w rwnmwns me we me 3 Wm we m mm m an same We emean nemmned aslume xeduce hrexinwe theS e unwary am e we and Exlcex Here 5 an enmv e 5mva bluxand map mmquot neseuwe e wme WWWWW anaeseqwenwees men we mwmms iwumms WM A Illnmda Wu We w e mam m abaulmenewde nman A amemmaamsae maursalmresha dmv amnwanwwumnm Wewwma r we wowewe my em mmemberwu wweemwmw m empessmwmemew me e w an abyea when Wu ca 3 myan Us campvehensmns Here we me e we mmquot m usuhem m ww e We mm m 5 e m ewes ler am che we m wem a mai emre v WM 5 abyens here 5mm As we warkwnhmnwans wem wed SwenMl memab wmmmk bmn mes ivam mum madmes m Sva caHewans mes We were quotauspice mmu km amadu es A He namsvacusyusla mummy 5 me m we vanabk 7am Scapan lllvenwecrezlemndmns mm mamaquot em namewace Wm M wde avamun mm quotms venemmmmaymmmw mm mum mm s quotauspice vm usuhuemswvuarekrm anamsvacewhem mm 1 sm vm mmaym sm hasmsc vmv N eswhen Wm WM quotnames MW see whmhavvens wnhmnwans Smpmg Mes Wm saw m me my usesm Mes mm werenusamseamhedm mmm mam unwans manV men v abahhe bum m 2 Bvdemmnameass vrmemscrezlearmanve au 3 mam dedamans quotup assvned namesmmescavem an endasmv rmde s Scapan W secand sun mmsha dmv mm we We m mm Ldaesnm mm mth mam Wm m Mes dues mmquot mm m m n mums v m m m mswpus mm swam ummam aka descnwmwenauvh 7mmquot u m wvan n and mm m Va camems as M mm W quotauspice 5m mm m mama n m mm mums mhersare hummus vquot Lacs versusg aha scape lllvenvvuwme unwary meenc asmvmadu e snsvhbm We mme svaad mne newmm mm mm hrvumewu a e wme Wu meme 3 new ac swve mg sgwmwmmmm memeewmemsm aeuwwmwm a wmmewmwm wmm use eachnew ca mew meme me Wu mquot m mmquot wsesswmeww be ac Wu sveu u vdedamnwbm nemesmeneww hm quotmassVnedm mmquot m emeranc asmv ac s V ab sar bum ms Em Mess caus nm dai39s myan mwwem duned Nvle m mneme scape we u seemem m m Lacs v G aba Here we m mm We wmnsemuwme w w w haw P lhanresvhls nme aiexamwe w weewm e vanabk wwa we vmduce smegma mg gem mm m nuke War we mm read Algamem m ammng wVwess mm mans mm m vammmnv me mew Numem syntax 5 mth wvarum m Rnwmbe em hawemr w W m me 195 hrwa amen quotadds Algamem m ammng m N eshrass vmnv wamesm mmm caHxa mm m Amman named iwumemsbvvasman 2 Assvmmm mmme we me 3 ksvnem unnamed iwumemsm a39namecawanem o ksvnem named mum quotname wmvanem 5 ugmammmumsmmmms Obye ranemed ngvammmg Wm usedmuem am We Mmmuvhamaur mm mmquot wnhmn Muwmm mm mm mm IIIm e we mannqu Ma ana wssanrwnham exvhcmvmscussmv abreasardassesaranvmmm evlmua h mm undemndxmswmmvamm imam m an s m cm mpmmumgm mgquot was w Fan2v mm aumm canmmhQ munpr Wm 5mm mmc udemesemam v u v m m m assvmmms m omen anemed pmgmmng Duryusu cman hrmnamnsmwhmcade reuse and abasc vammmv i luhmbmkei vmb emmm ma er camvanems m manakbemndabyenanemmmms am new we m awamxe m WWW and mm mm mm camsl m mm humming mm am anemd w mm mm m mummy Statistics 202a Data Technologies and Programming Principles This course is not A class in Computational Statistics We will not explicitly cover matrix decompositions or algorithms for solving least squares problems Statistics 202a Instructor Mark Hansen 8951 Mathematical Sciences x68375 cocteaustatucaedu Office Hours TuesThurs TBD or by appointment Grading 80 Projects 20 inclass participation This course is not A class in Applied Statistics We will not explicitly cover algorithms for fitting a linear model interrogating the fit using diagnostics to suggest various elaborations Tms cuuvse snm Adass m mm same We Wu m1 Emhm vmvev armmgyemgmmg avgs smme behawm meswmnvs mesmmnvevgeme m mmmax v WW7 Om Om ahmwmcampme wpesanumnsmg kmsm News me an penuvm ahmwmcampme mmysawewnmmn mm as 12 m an be vwmm has2 smug we are baweummg mmenams and mnsmed a vv lnensmeane needsa mm Tms cuuvse 5 Am mammaryth Mes m ewlzramry ampume thecampMatmna smHsneueme mndm EH Phases m a sansan gnaw Same msmw R A My sanen weaned as me snme mashmmnam gme m 2cm mm sansms Emma Fxsh sansms was an mgemaus mHeman m ad m devmesquot swam 1 996 SmysmaMemudsfm eseam erkers1 55mvasmanv Mme hasctecmmesstm mm m mmduaavwenbmks n 57mquot w mermoxun m v mman mmxw wn Same mswrv He 5 respmsxh e my weatmg a mmmmmma WWW my sahsnx hm equawmpman was msxmpa an m ana yss What mppenedbetv em rmwsmg he192 s and 3mm mm he19m s am ms7 mm anus mmmemgd mm W Momma he had Wesmed au he knew SW9 51 Juhnvmaermmymwsznnn Jam mksv 5 mm maneev msmmmgwam pmmue 2 WM nms m WWW 0m nw thetvansstw vusdevdamd mm m Mm 970 mxsv am yapmm ma s m nwsuhewam 35m mmmm am qmybemmesme em anng mamaser m swam msmmma mamwame Emma 3 mm demxptwetecmmes Bythe 9m a hew scmpmenesuums v evegenemw Presmled m mvammvytenbaaks ava ah eat maymmsnmmns re duemmkev stsy exsmamwe advumhng mm mam mnaem mm m The cumpmev m mumquot v he Gamma mum mmmusm 5 vvadmed Aulammed mm m n mm mmMmm Mmmyam myquot m w my m sums am wecan wammanv mwss we can made sandpewvm camvulmmm exw m mm m uncwuwhameammwmmvuamuus Hume cumpmevs and me Mamet mm mm camvmev m 1975 MwsAnauaEUU n1Q77APD e Mvaduces heAW eHa a pm am 195 axamAM namannm mmsmmm mm Wm smpsmm n m ds vnedhvlh 214 2 m App eu mu m m YheAPv e Mammava appears m m4 MmmsanVImdwus1D5MD5VME5 And me 197m Hume cumpmevs and me Mamet m m gang hue 5 mm a mum cmmv YheveavesmaHhmvawe u camvukvsmvhanes m cm vaunamen m Meme m mm nauanwmewbev am mm mm wnhwue essaccessbecammvslandam Mmesamehme Whammy mumquot m m Pamcmavmusexsamakdwnhenwanmenmmannmmvave an vmnvasma hevamhan Mhevmmmsemmnmams shaumnumm havesame mm an m Wm mama
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'