CS 111: Program Desig I Lecture 16: Module Review, Ecodigs, Lists Robert H. Sloa & Richard Warer Uiversity of Illiois at Chicago October 18, 2016
Last time Dot otatio ad methods Padas: user maual poit of view modules
Today Modules: Review with oe more example Ecodigs Lists
MODULES: REVIEW, 1 MORE EXAMPLE (RANDOM)
Modules import to make it available access it through the dot otatio (like methods) We'll briefly look at oe iterestig module that is from the stadard library: radom
A iterestig module: radom >>> import radom >>> for i i rage(5):... prit(radom.radom())... 0.12636664029165268 0.2821272889535512 0.6160031940187543 0.28609006981908525 0.6277074518401735 Notice: We're usig the fuctio amed radom from the module amed radom, hece radom.radom()
Radomly choosig words from a list >>> for i i rage(5):... prit(radom.choice(["here", "is", "a", "list", "of", "words", "i", "radom", "order"]))... list words i Here list
How ofte do you look at xkcd A Ususally or always B Oce i a while C Never D I've ever heard of xkcd
Radomly geeratig laguage Give a list of ous, verbs that agree i tese ad umber, ad object phrases that all match the verb, We ca radomly take oe from each to make seteces.
import radom def excuse(): excuse = ["I did't kow I was i this class", "I thought I already graduated", "I got stuck i a blizzard"] bignum = ["4", "17", "like a billio", "mega", "tos of"] lottawork = ["midterms", "Ph.D. theses", "programs"] prit ("I eed a extesio because", radom.choice(excuse), "ad I had", radom.choice(bignum), radom.choice(lottawork), "to do.") Side ote: Good example of a fuctio that should have 0 iputs ad o retur value.
Ruig radom setece geerator >>> excuse() I eed a extesio because I thought I already graduated ad I had like a billio programs to do. >>> excuse() I eed a extesio because I got stuck i a blizzard ad I had 4 programs to do. >>> excuse() I eed a extesio because I got stuck i a blizzard ad I had 17 programs to do. >>> excuse() I eed a extesio because I thought I already graduated ad I had tos of programs to do. >>> excuse() I eed a extesio because I did t kow I was i this class ad I had 17 Ph.D. theses to do.
Questios about lab or homework?
ENCODINGS
Ecodigs agai Recall that the smallest uit i a computer is the bit Oe bit ca take o 2 possible values: 0 or 1 Two bits ca take o 4 possible values: 00, 01, 10, or 11 Three bits ca take o 8 possible values: 000, 001, 010, 011, 100, 101, 110, 111
How may distict values ca 4 bits take o? A. 4 B. 8 C. 9 D. 13 E. 16
Be Bitdiddle says bits ca take o 2 times as may values as -1 bits = 2 values
How may distict values ca a byte take o (Recall that a byte = 8 bits) A. 2 B. 8 C. 64 D. 128 E. 256
Ecodig characters i bytes ASCII: Use 1 byte to ecode 95 pritig characters The oes o every computer keyboard to this day Pretty much all ecodigs agree with ASCII o those 95 characters
But What about Vigeère? А что насчет Aрабского?
Ecodig more characters Uicode: over 128,000 characters coverig 135 moder ad historical scripts, ad symbols 2 bytes ot eough for all of it q 2 16 is "oly" 65,536 Various character ecodigs for all or a subset Pytho supports Uicode
Ecodigs of files i Uicode Two fairly commo ecodigs of Uicode (i.e., bit patter character correspodeces) q q UTF-8 (Pytho's default guess): multi-byte, variable umber of bytes (1 4 bytes/character) ISO-8859-1: 1 byte, so ca ecode limited # of characters; i fact ecodes Lati-1 ASCII plus most acceted characters plus commo symbols: 195 characters covers all of Eglish ad may commo Europea laguages ½¼ ± ÀÁÂÃÄ Æ Ç ÈÉÊË Ø àáâãäå æ ç èé N.B. Oly a issue for files
Which ecodig? It's pretty uusual to eed to give Pytho the ecodig argumet whe opeig a file Pytho defaults to UTF-8, which agrees with ASCII o ASCII characters, ad most text files are ASCII Ad very may of the rest are UTF-8 But some are ISO-8859-1 q For websites, about 5 or 6%
Supreme Court Database that we are workig with is ISO-8859-1 q Almost etirely ASCII characters, but uses symbol i places
LISTS
Next big project: Fiish Web Crawler We will eed to make more extesive use of lists tha we have up util ow Recall our strategy is goig to be somethig like
Crawl all pages reachable from start List of pages to visit, iitially start while that list is ot empty: q Take a page from the list q Get its text # eed to lear how to do this q q q remove that page from to-visit list, add it to already-visited lst Get all the liks i that page for each lik if ot already i visited list add it
Ad besides Lists i Pytho are just awesome
Lists versus Strigs List Elemets ca be ay type Mutable Heterogeeous elemets Ca be ested i other lists Strig Elemets are characters Immutable Homogeous elemets
Recall Lists are data structures that let us store collectios of data i sequece with idices
Remember the smallest "" # Empty strig, could also be writte '' [] # empty list
A = [2,3,5] B = ["Brea"] C = A + B prit(c) will result i: A. [2,3,5] B. [2,3,5,"Brea"] C. ["2","3","5","Brea] D. This will cause a error E. I do t kow
List Fuctios These are ot methods; there are also list methods, e.g., apped le: legth of a list (i.e., umber of elemets) + will cocateate lists mi, max: miimum or maximum of list sum: sum of the elemets i the list q E.g., sum([2, 3, 5]) à 10
Importat: Lists are mutable >>> years= [1788, 1800, 1860, 1932] >>> years[0] = years[0] + 4 >>> prit(years) [1792, 1800, 1860, 1932]
What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' prit(le(lst)) A. 3 B. 4 C. 23 D. 30 E. No output; error i 2 d lie of code
What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' ow lst has become ['abc.com', 'fox.com', 'msbc.com'] prit(le(lst)) A. 3 B. 4 C. 23 D. 30
What will this output? def ic(ls, x): x = x + 1 for i i rage(le(ls)): ls[i] = ls[i] + 1 ls = [2, 6, 7] x = 5 ic(ls, x) prit(ls, " ", x) Clicker prit(ls, " ", x) A [2,6,7] 5 B [3,7,8] 6 C [3,7,8] 5 D [2,6,7] 6 E I do t kow
Parameter passig (review, Chapt. 6.3) Recall: Assigmet statemet iside fuctio creates local variable q Distict object from ay outside fuctio; exists oly iside fuctio; ca't be used outside Ad formal parameters of fuctios are also local variables That's all there is to thik about ad all that's worth kowig about local variables q As log as a referece to immutable object is what is passed i as actual parameter
Parameter passig: lists as actual parameters Passig list as argumet to fuctio passes referece to that list, ot a copy q Chages made by fuctio will be visible afterwards i caller's scope! Fuctios that chage lists passed to them as parameters called modifiers; chages they make are called side effects (of callig the fuctio)