Download - CS 61A Lecture 10
![Page 1: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/1.jpg)
CS 61A Lecture 10
![Page 2: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/2.jpg)
Announcements
![Page 3: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/3.jpg)
Lists
['Demo']
![Page 4: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/4.jpg)
Working with Lists
4
![Page 5: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/5.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
![Page 6: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/6.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 7: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/7.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 8: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/8.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 9: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/9.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 10: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/10.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 11: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/11.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
>>> getitem(digits, 3) 8
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 12: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/12.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
Concatenation and repetition
>>> getitem(digits, 3) 8
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 13: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/13.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
>>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
Concatenation and repetition
>>> getitem(digits, 3) 8
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 14: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/14.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
>>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
Concatenation and repetition
>>> getitem(digits, 3) 8
>>> add([2, 7], mul(digits, 2)) [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 15: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/15.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
Nested lists
>>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
Concatenation and repetition
>>> getitem(digits, 3) 8
>>> add([2, 7], mul(digits, 2)) [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 16: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/16.jpg)
Working with Lists
>>> digits = [1, 8, 2, 8]
4
The number of elements>>> len(digits) 4
An element selected by its index>>> digits[3] 8
Nested lists>>> pairs = [[10, 20], [30, 40]] >>> pairs[1] [30, 40] >>> pairs[1][0] 30
>>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
Concatenation and repetition
>>> getitem(digits, 3) 8
>>> add([2, 7], mul(digits, 2)) [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]
>>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
![Page 17: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/17.jpg)
Containers
![Page 18: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/18.jpg)
Containers
6
![Page 19: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/19.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
![Page 20: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/20.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]
![Page 21: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/21.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]>>> 1 in digitsTrue
![Page 22: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/22.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]>>> 1 in digitsTrue>>> 8 in digitsTrue
![Page 23: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/23.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]>>> 1 in digitsTrue>>> 8 in digitsTrue>>> 5 not in digitsTrue
![Page 24: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/24.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]>>> 1 in digitsTrue>>> 8 in digitsTrue>>> 5 not in digitsTrue>>> not(5 in digits)True
![Page 25: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/25.jpg)
Containers
Built-in operators for testing whether an element appears in a compound value
6
>>> digits = [1, 8, 2, 8]>>> 1 in digitsTrue>>> 8 in digitsTrue>>> 5 not in digitsTrue>>> not(5 in digits)True
(Demo)
![Page 26: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/26.jpg)
For Statements
(Demo)
![Page 27: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/27.jpg)
Sequence Iteration
8
![Page 28: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/28.jpg)
Sequence Iteration
def count(s, value): total = 0 for element in s:
if element == value: total = total + 1 return total
8
![Page 29: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/29.jpg)
Sequence Iteration
def count(s, value): total = 0 for element in s:
if element == value: total = total + 1 return total
Name bound in the first frame of the current environment
(not a new frame)
8
![Page 30: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/30.jpg)
For Statement Execution Procedure
9
![Page 31: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/31.jpg)
For Statement Execution Procedure
for <name> in <expression>: <suite>
9
![Page 32: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/32.jpg)
For Statement Execution Procedure
for <name> in <expression>: <suite>
1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
9
![Page 33: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/33.jpg)
For Statement Execution Procedure
for <name> in <expression>: <suite>
1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
2. For each element in that sequence, in order:
9
![Page 34: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/34.jpg)
For Statement Execution Procedure
for <name> in <expression>: <suite>
1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
2. For each element in that sequence, in order:
A. Bind <name> to that element in the current frame
9
![Page 35: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/35.jpg)
For Statement Execution Procedure
for <name> in <expression>: <suite>
1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
2. For each element in that sequence, in order:
A. Bind <name> to that element in the current frame
B. Execute the <suite>
9
![Page 36: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/36.jpg)
Sequence Unpacking in For Statements
10
![Page 37: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/37.jpg)
Sequence Unpacking in For Statements
>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
>>> same_count = 0
10
![Page 38: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/38.jpg)
Sequence Unpacking in For Statements
>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
>>> same_count = 0
A sequence of fixed-length sequences
10
![Page 39: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/39.jpg)
Sequence Unpacking in For Statements
>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
>>> same_count = 0
>>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1
>>> same_count 2
A sequence of fixed-length sequences
10
![Page 40: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/40.jpg)
Sequence Unpacking in For Statements
>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
>>> same_count = 0
>>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1
>>> same_count 2
A sequence of fixed-length sequences
A name for each element in a fixed-length sequence
10
![Page 41: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/41.jpg)
Sequence Unpacking in For Statements
>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
>>> same_count = 0
>>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1
>>> same_count 2
A sequence of fixed-length sequences
A name for each element in a fixed-length sequence
Each name is bound to a value, as in multiple assignment
10
![Page 42: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/42.jpg)
Ranges
![Page 43: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/43.jpg)
The Range Type
A range is a sequence of consecutive integers.*
12
![Page 44: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/44.jpg)
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.12
![Page 45: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/45.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.12
![Page 46: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/46.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
12
![Page 47: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/47.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
12
![Page 48: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/48.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
12
![Page 49: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/49.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
12
![Page 50: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/50.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
12
![Page 51: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/51.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
Element selection: starting value + index
12
![Page 52: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/52.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
>>> list(range(-2, 2)) [-2, -1, 0, 1]
>>> list(range(4)) [0, 1, 2, 3]
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
Element selection: starting value + index
12
![Page 53: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/53.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
>>> list(range(-2, 2)) [-2, -1, 0, 1]
>>> list(range(4)) [0, 1, 2, 3]
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
Element selection: starting value + index
List constructor
12
![Page 54: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/54.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
>>> list(range(-2, 2)) [-2, -1, 0, 1]
>>> list(range(4)) [0, 1, 2, 3]
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
Element selection: starting value + index
List constructor
Range with a 0 starting value
12
![Page 55: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/55.jpg)
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...
The Range Type
>>> list(range(-2, 2)) [-2, -1, 0, 1]
>>> list(range(4)) [0, 1, 2, 3]
A range is a sequence of consecutive integers.*
* Ranges can actually represent more general integer sequences.
range(-2, 2)
Length: ending value - starting value
Element selection: starting value + index
List constructor
Range with a 0 starting value
(Demo)
12
![Page 56: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/56.jpg)
List Comprehensions
![Page 57: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/57.jpg)
List Comprehensions
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n', 'o', 'p']>>> [letters[i] for i in [3, 4, 6, 8]]
![Page 58: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/58.jpg)
List Comprehensions
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n', 'o', 'p']>>> [letters[i] for i in [3, 4, 6, 8]]
['d', 'e', 'm', 'o']
![Page 59: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/59.jpg)
List Comprehensions
14
![Page 60: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/60.jpg)
List Comprehensions
[<map exp> for <name> in <iter exp> if <filter exp>]
14
![Page 61: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/61.jpg)
List Comprehensions
[<map exp> for <name> in <iter exp> if <filter exp>]
Short version: [<map exp> for <name> in <iter exp>]
14
![Page 62: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/62.jpg)
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:
14
![Page 63: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/63.jpg)
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:
1. Add a new frame with the current frame as its parent
14
![Page 64: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/64.jpg)
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:
1. Add a new frame with the current frame as its parent
2. Create an empty result list that is the value of the expression
14
![Page 65: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/65.jpg)
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:
1. Add a new frame with the current frame as its parent
2. Create an empty result list that is the value of the expression
3. For each element in the iterable value of <iter exp>:
14
![Page 66: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/66.jpg)
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:
1. Add a new frame with the current frame as its parent
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
14
![Page 67: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/67.jpg)
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:
1. Add a new frame with the current frame as its parent
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 the result list
14
![Page 68: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/68.jpg)
Strings
![Page 69: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/69.jpg)
Strings are an Abstraction
16
![Page 70: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/70.jpg)
Strings are an Abstraction
Representing data:
'200' '1.2e-5' 'False' '[1, 2]'
16
![Page 71: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/71.jpg)
Strings are an Abstraction
Representing data:
'200' '1.2e-5' 'False' '[1, 2]'
Representing language:
"""And, as imagination bodies forth The forms of things unknown, the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """
16
![Page 72: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/72.jpg)
Strings are an Abstraction
Representing data:
'200' '1.2e-5' 'False' '[1, 2]'
Representing language:
"""And, as imagination bodies forth The forms of things unknown, the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """
Representing programs:
'curry = lambda f: lambda x: lambda y: f(x, y)'
16
![Page 73: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/73.jpg)
Strings are an Abstraction
Representing data:
'200' '1.2e-5' 'False' '[1, 2]'
Representing language:
"""And, as imagination bodies forth The forms of things unknown, the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """
Representing programs:
'curry = lambda f: lambda x: lambda y: f(x, y)'
(Demo)
16
![Page 74: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/74.jpg)
String Literals Have Three Forms
>>> 'I am string!' 'I am string!'
>>> "I've got an apostrophe" "I've got an apostrophe"
>>> ' ' ' '
17
![Page 75: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/75.jpg)
String Literals Have Three Forms
>>> 'I am string!' 'I am string!'
>>> "I've got an apostrophe" "I've got an apostrophe"
>>> ' ' ' '
Single-quoted and double-quoted strings are equivalent
17
![Page 76: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/76.jpg)
String Literals Have Three Forms
>>> 'I am string!' 'I am string!'
>>> "I've got an apostrophe" "I've got an apostrophe"
>>> ' ' ' '
>>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'
Single-quoted and double-quoted strings are equivalent
17
![Page 77: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/77.jpg)
String Literals Have Three Forms
>>> 'I am string!' 'I am string!'
>>> "I've got an apostrophe" "I've got an apostrophe"
>>> ' ' ' '
>>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'
A backslash "escapes" the following character
Single-quoted and double-quoted strings are equivalent
17
![Page 78: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/78.jpg)
String Literals Have Three Forms
>>> 'I am string!' 'I am string!'
>>> "I've got an apostrophe" "I've got an apostrophe"
>>> ' ' ' '
>>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'
"Line feed" character represents a new line
A backslash "escapes" the following character
Single-quoted and double-quoted strings are equivalent
17
![Page 79: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/79.jpg)
Dictionaries
{'Dem': 0}
![Page 80: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/80.jpg)
Limitations on Dictionaries
19
![Page 81: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/81.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
19
![Page 82: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/82.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
19
![Page 83: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/83.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
•A key of a dictionary cannot be a list or a dictionary (or any mutable type)
19
![Page 84: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/84.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
•A key of a dictionary cannot be a list or a dictionary (or any mutable type)
•Two keys cannot be equal; There can be at most one value for a given key
19
![Page 85: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/85.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
•A key of a dictionary cannot be a list or a dictionary (or any mutable type)
•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
19
![Page 86: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/86.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
•A key of a dictionary cannot be a list or a dictionary (or any mutable type)
•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 part of the dictionary abstraction
19
![Page 87: CS 61A Lecture 10](https://reader031.vdocuments.us/reader031/viewer/2022021919/620fd9e9e7e9fa207b6ac839/html5/thumbnails/87.jpg)
Limitations on Dictionaries
Dictionaries are unordered collections of key-value pairs
Dictionary keys do have two restrictions:
•A key of a dictionary cannot be a list or a dictionary (or any mutable type)
•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 part of the dictionary abstraction
If you want to associate multiple values with a key, store them all in a sequence value
19