Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.

Similar presentations


Presentation on theme: "Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions."— Presentation transcript:

1 Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions

2 Review To see both code & I/O: Window  Tile If your program has functions, how does the interpreter know where your program starts? When you write a loop with range: for i in range (1, 10) –i retains its last value after the loop (9) Look up something? Documentation: Python Standard Library, see “index” Link at top right of page.

3 Tokenizing Important skill in reading text input –Input file may be in specific format Two techniques –String’s split( ) function –Python’s regular expression library  more powerful

4 String split( ) Simple to use Good when there is a certain pattern of characters you want as a delimiter s = “moo--goo---gai--pan“ s.split(“-”) [‘moo’,’’,’goo’,’’,’’,’gai’,’’,’pan’] s.split(“--“) [‘moo’,’goo’,’-gai’,’pan’] s.split(“---”) [‘moo--goo‘,’gai--pan’]

5 Using re More often, we want to handle multiple possible delimiters. –Like Java’s StringTokenizer or C’s strtok( ) –Extracting words by ignoring any and all punctuation See handout: tokenize.py –tok = re.compile( )  takes your set of delimiters and creates a “regular expression object” i.e. tokenizer Don’t forget the brackets around delimiters. –tok.split(my_string)  gives list of tokens

6 Random #s Python has a built-in library (module) called random At top of program, say: import random Two most important functions –random.uniform(a, b) returns a random real number between a and b inclusive –random.randint(a, b) does the same for int


Download ppt "Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions."

Similar presentations


Ads by Google