list, tuple, string, linked listcontents.kocw.net/kocw/document/2014/handong/kimyoungsup/9.pdf2....

20
ITP 20005 Programming Languages Chapter 2 Building Abstractions with Data 2.1 Introduction 2.2 Data Abstraction 2.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, Google 2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia and many on-line resources. Prof. Youngsup Kim, [email protected] , 2014 Programming Languages, CSEE Dept., Handong Global University

Upload: others

Post on 03-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University

Page 2: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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, [email protected], 2014 Programming Languages, CSEE Dept., Handong Global University

Python List built-in methods, accessing elements, selection, slicing, recursive functions,list comprehension

Page 3: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

3

2.3 The Sequence Abstraction

Christ

Page 4: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

4

2.3 The Sequence Abstraction

Christ

in whom are hidden all the treasures of wisdom and knowledge

Col2:3

Page 5: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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

Page 6: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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

Page 7: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

7

2.3 The Sequence Abstraction

red, orange, yellow, green, blue, indigo, violet.

Page 8: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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)

Page 9: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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:

Page 10: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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.

Page 11: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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.

Page 12: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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.

Page 13: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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

Page 14: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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

Page 15: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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()?

Page 16: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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

Page 17: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

17

2.3 Python Sequence Iteration

For Statement Execution Procedure

for <name> in <expression>:

<suite>

Page 18: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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.

Page 19: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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>.

Page 20: List, Tuple, String, Linked Listcontents.kocw.net/KOCW/document/2014/handong/kimyoungsup/9.pdf2. MITx OPENCOURSEWARE(OCW) 6.00 SC Lecture 3. UC Berkley CS61A Lecture 4. Python, Wikipedia

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]