2014Sp-CS10-L23-MB-PS-Besides-Blocks-IIcs10/sp14/Lectures/L23 - Besides Blo… · Title:...
Transcript of 2014Sp-CS10-L23-MB-PS-Besides-Blocks-IIcs10/sp14/Lectures/L23 - Besides Blo… · Title:...
-
UC Berkeley EECS
Head TA Michael Ball
The Beauty and Joy of Computing
Besides Blocks Python
Session 2: Data Structures
UC Berkeley EECS
TA Peter Sujan
(thanks to Glenn Sugden for the first version of these slides)
is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (2)
Garcia
§ Review (and some new introductions) § Sequences
ú Operators § Sets
ú Operators § Dictionaries § Higher-Order Functions § Let’s Re-visit the midterm Exam!
Data Structures (Overview)
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (3)
Garcia
§ Typing, Build-In Types ú Int, function, string, list, etc
§ Variables § Looping and Conditionals
ú for loops, ú While loops
§ Functions ú Recursion
§ This week’s content ú Sequences, APIs
Review
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (4)
Ball Sujan
Sequences
§ Contain and ORDERED set of data § str – short for a “string of text” § list - [‘a’, ‘group’ , ‘of’, ‘items’] § range(start, stop, step) § tuple – a list that can’t be modified § Supports very easy iteration: § for item in sequence:
print(item)
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (5)
Ball Sujan
Sequence (General) Operators
§ elem in & not in sequence § + & * § slice [START:END:STEP] § len() § min() & max() § Even map() filter() & reduce()! § count(item) § Many, many more:
http://docs.python.org/library/stdtypes.html#typesseq
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (6)
Ball Sujan
Strings and String Operators § Sequence (or “list” or “array”) of chars § Quoting
ú Single Quotes, Double Quotes ú Triple Quotes (this keeps formatting and line
breaks) § Concentration, finding length, etc.
ú help(“string”)
§ Slicing Supported [START:END:STEP] § http://docs.python.org/library/
stdtypes.html#string-methods
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (7)
Ball Sujan
Lists § Collection of any type
ú Including itself! § Indexing (Snap!: item() of []) § Modifying (replace item () of [] with ())
§ Slicing and slicing notation (i.e. [::]) ú Exactly the same as string notation!
§ Operators ú append(x), insert(i,x), count(x), sort(), etc.
§ http://docs.python.org/library/stdtypes.html#mutable-sequence-types
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (8)
Ball Sujan
Dictionaries § Very fast access (by key, not number) § “Map” from a key to a value § Syntax
ú { key1 : value1, key2 : value2, … }
§ Adding elements ú dict[key] = value
§ Accessing elements; dict[key] § Keys
ú Looking for specific keys (has_key() & “in”) ú Iterating over (iterkeys())
-
UC Berkeley “The Beauty and Joy of Computing” : Besides Blocks Python Section 2 (9)
Ball Sujan
More Information
§ Sequences & Methods ú http://docs.python.org/library/stdtypes.html
§ Coding Bat (Great practice!) ú http://codingbat.com/python
§ Google’s Python Class ú http://code.google.com/edu/languages/google-
python-class/ § Exercises (More practice!)
ú http://code.google.com/edu/languages/google-python-class/exercises/basic.html