Post on 03-Aug-2020
ITP 20005 Programming LanguagesChapter 2 Building Abstractions with Data
2.1 Introduction2.2 Data Abstraction2.3 Sequences
List, Tuple, String, Linked List
Major references: 1. Structure and Interpretation of Computer Programs(SICP) by Abelson and Sussman, MIT
Composing Programs by John DeNero, Google2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia and many on-line resources.Prof. Youngsup Kim, idebtor@handong.edu, 2014 Programming Languages, CSEE Dept., Handong Global University
ITP 20005 Programming LanguagesChapter 2 Building Abstractions with Data
2.1 Introduction2.2 Data Abstraction2.3 Sequences
List, Tuple, String, Linked List
Major references: 1. Structure and Interpretation of Computer Programs(SICP) by Abelson and Sussman, MIT
Composing Programs by John DeNero, Google2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia and many on-line resources.Prof. Youngsup Kim, idebtor@handong.edu, 2014 Programming Languages, CSEE Dept., Handong Global University
Python List built-in methods, accessing elements, selection, slicing, recursive functions,list comprehension
3
2.3 The Sequence Abstraction
Christ
4
2.3 The Sequence Abstraction
Christ
in whom are hidden all the treasures of wisdom and knowledge
Col2:3
5
2.3 The Sequence Abstraction
Christ
in whom are hidden all the treasures of wisdom and knowledge
Col2:3
Taste and see that the LORD is good;Ps34:8
6
2.3 The Sequence Abstraction
Christ
in whom are hidden all the treasures of wisdom and knowledge
Col2:3
Taste and see that the LORD is good;Ps34:8
The fear of the LORD is the beginning of knowledge,
but fools despise wisdom and discipline.
Pro1:7
7
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
8
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
There isn't just one sequence type (in Python or in general)
9
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
There isn't just one sequence type (in Python or in general)
This abstraction is a collection of behaviors:
10
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
There isn't just one sequence type (in Python or in general)
This abstraction is a collection of behaviors:
Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non‐negative integer index less than its length, starting at 0 for the first element.
11
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
There isn't just one sequence type (in Python or in general)
This abstraction is a collection of behaviors:
0 , 1 , 2 , 3 , 4 , 5 , 6 .
Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non‐negative integer index less than its length, starting at 0 for the first element.
12
2.3 The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
There isn't just one sequence type (in Python or in general)
This abstraction is a collection of behaviors:
Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non‐negative integer index less than its length, starting at 0 for the first element.
0 , 1 , 2 , 3 , 4 , 5 , 6 .
The sequence abstraction is shared among several types,including tuples.
13
2.3 Python Sequence Abstraction
Built-in sequence types provide the following behavior.
>>> a = (1, 2, 3)>>> b = tuple([4, 5, 6, 7])
Type‐specificconstructor
>>> len(a), len(b)(3, 4)
length
Elementselection
>>> a[1], b[‐1](2, 7)
Slicing>>> a[1:3], b[1:1], a[:2], b[1:]((2, 3), (), (1, 2), (5, 6, 7))
Membership>>> 2 in a, 4 in a, 4 not in b(True, False, False)
tuple
list : variable length,mutable objectstay tuned for more
tuple
14
2.3 Python Sequence Abstraction
Built-in sequence types provide the following behavior.
>>> a = (1, 2, 3)>>> b = tuple([4, 5, 6, 7])
Type‐specificconstructor
>>> len(a), len(b)(3, 4)
length
Elementselection
>>> a[1], b[‐1](2, 7)
Slicing>>> a[1:3], b[1:1], a[:2], b[1:]((2, 3), (), (1, 2), (5, 6, 7))
Membership>>> 2 in a, 4 in a, 4 not in b(True, False, False)
tuple
list : variable length,mutable objectstay tuned for more
tuple
Counting from the end; -1 is the last element
15
2.3 List as a Type in Python
Tuple: fixed-length, immutable sequence of Python objects
List: variable-length, and mutable
>>> a = [3, 1, 2]>>> a[3, 1, 2]>>> len(a)3>>> a[1]1>>> c, d = a, a[:]>>> a, c, d([3, 1, 2], [3, 1, 2], [3, 1, 2])>>> c[0] = 4>>> a, c, d([4, 1, 2], [4, 1, 2], [3, 1, 2])>>> d[0] = 5>>> a, c, d([4, 1, 2], [4, 1, 2], [5, 1, 2])>>> a[1:2] = [7, 8, 9]>>> a, c, d([4, 7, 8, 9, 2], [4, 7, 8, 9, 2], [5, 1, 2])
Create a list using square brackets:
Lists are sequences.
Bind another name to a list or a slice of a list.
Modify contents of a list.
wut()?
16
2.3 Python Sequence Iteration
Python has a special statement for iterating over the elements in a sequence
def count(s, value):
total = 0
for elem in s:
if elem == value:
total += 1
return total
Name ‘elem’ bound in the first frame of the current environment
17
2.3 Python Sequence Iteration
For Statement Execution Procedure
for <name> in <expression>:
<suite>
18
2.3 Python Sequence Iteration
For Statement Execution Procedure
for <name> in <expression>:
<suite>
1. Evaluate the header <expression>, which must yield an iterable value.
19
2.3 Python Sequence Iteration
For Statement Execution Procedure:
for <name> in <expression>:
<suite>
1. Evaluate the header <expression>, which must yield an iterable value.
2. For each element in that sequence, in order:• Bind <name> to that element in the first frame of the current
environment.• Execute the <suite>.
20
2.3 List Comprehensions see 2.3.3
One large expression that combines mapping and filtering to produce an iterable.
• Evaluates to an iterable.• <iter exp> is evaluated when the generator expression is evaluated.• Remaining expressions are evaluated when elements are accessed.
No‐filter version: [ <map exp> for <name> in <iter exp> ]
Stay tuned: Precise evaluation rule introduced in Chapter 4.
[<map exp> for <name> in <iter exp> if <filter exp>]
[Demo]