61a lecture 12
play

61A Lecture 12 Project 2 due Thursday 10/10 @ 11:59pm Guerrilla - PDF document

Announcements Homework 3 due Tuesday 10/1 @ 11:59pm Optional Hog Contest due Thursday 10/3 @ 11:59pm Homework 4 due Tuesday 10/8 @ 11:59pm 61A Lecture 12 Project 2 due Thursday 10/10 @ 11:59pm Guerrilla Section 2 this Saturday


  1. Announcements • Homework 3 due Tuesday 10/1 @ 11:59pm • Optional Hog Contest due Thursday 10/3 @ 11:59pm • Homework 4 due Tuesday 10/8 @ 11:59pm 61A Lecture 12 • Project 2 due Thursday 10/10 @ 11:59pm • Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda Monday, September 30  Topics: Data abstraction, sequences, non-local assignment  Meet outside Soda 306 2 Sequence Iteration def count(s, value): total = 0 for element in s: For Statements Name bound in the first frame of the current environment (not a new frame) if element == value: total = total + 1 return total (Demo) 4 For Statement Execution Procedure Sequence Unpacking in For Statements A sequence of for <name> in <expression>: fixed-length sequences <suite> >>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0 1.Evaluate the header <expression>, which must yield an iterable value (a sequence). A name for each element in a Each name is bound to a value, as in 2.For each element in that sequence, in order: multiple assignment fixed-length sequence A. Bind <name> to that element in the first frame of the current environment. >>> for x, y in pairs: if x == y: same_count = same_count + 1 B. Execute the <suite>. >>> same_count 2 5 6

  2. The Range Type A range is a sequence of consecutive integers. * ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Ranges Length : ending value - starting value (Demo) Element selection : starting value + index >>> tuple(range(-2, 2)) Tuple constructor (-2, -1, 0, 1) >>> tuple(range(4)) With a 0 starting value (0, 1, 2, 3) * Ranges can actually represent more general integer sequences. 8 Membership & Slicing The Python sequence abstraction has two more behaviors! Membership . >>> digits = (1, 8, 2, 8) >>> 2 in digits True Lists >>> 1828 not in digits True Slicing. >>> digits[0:2] Slicing creates a new object (1, 8) >>> digits[1:] ['Demo'] (8, 2, 8) http://docs.python.org/py3k/library/stdtypes.html#mutable-sequence-types 9 List Comprehensions [<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure: Dictionaries 1. Add a new frame extending the current frame. 2. Create an empty result list that is the value of the expression. 3. For each element in the iterable value of <iter exp>: A. Bind <name> to that element in the new frame from step 1. B. If <filter exp> evaluates to a true value, then add the value of <map exp> to {'Dem': 0} the result list. 11

  3. Limitations on Dictionaries Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions: • A key of a dictionary cannot be an object of a mutable built-in type. Identity and Equality • Two keys cannot be equal . There can be at most one value for a given key. This first restriction is tied to Python's underlying implementation of dictionaries. The second restriction is an intentional consequence of the dictionary abstraction. If you want to associate multiple values with a key, store them all in a sequence. (Demo) Example: http://goo.gl/5AbYNM 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend