Mutable Values Announcements Objects (Demo) Objects 4 Objects - - PowerPoint PPT Presentation

mutable values announcements objects
SMART_READER_LITE
LIVE PREVIEW

Mutable Values Announcements Objects (Demo) Objects 4 Objects - - PowerPoint PPT Presentation

Mutable Values Announcements Objects (Demo) Objects 4 Objects Objects represent information 4 Objects Objects represent information They consist of data and behavior, bundled together to create abstractions 4 Objects


slide-1
SLIDE 1

Mutable Values

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Objects

(Demo)

slide-4
SLIDE 4

Objects

4

slide-5
SLIDE 5

Objects

  • Objects represent information

4

slide-6
SLIDE 6

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions

4

slide-7
SLIDE 7

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes

4

slide-8
SLIDE 8

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python

4

slide-9
SLIDE 9

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:

4

slide-10
SLIDE 10

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs

4

slide-11
SLIDE 11

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs

4

slide-12
SLIDE 12

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object

4

slide-13
SLIDE 13

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes

4

slide-14
SLIDE 14

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes
  • A lot of data manipulation happens through object methods

4

slide-15
SLIDE 15

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes
  • A lot of data manipulation happens through object methods
  • Functions do one thing; objects do many related things

4

slide-16
SLIDE 16

Example: Strings

(Demo)

slide-17
SLIDE 17

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange

6

slide-18
SLIDE 18

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits

6

slide-19
SLIDE 19

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-20
SLIDE 20

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-21
SLIDE 21

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-22
SLIDE 22

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-23
SLIDE 23

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-24
SLIDE 24

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n)

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-25
SLIDE 25

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

6

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-26
SLIDE 26

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

6

(Demo)

0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

slide-27
SLIDE 27

Representing Strings: the Unicode Standard

7

slide-28
SLIDE 28

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

7

slide-29
SLIDE 29

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1

7

slide-30
SLIDE 30

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)

7

slide-31
SLIDE 31

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

7

slide-32
SLIDE 32

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order

7

slide-33
SLIDE 33

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

7

slide-34
SLIDE 34

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A

7

slide-35
SLIDE 35

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A DIE FACE-6

7

slide-36
SLIDE 36

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A DIE FACE-6 EIGHTH NOTE

7

slide-37
SLIDE 37

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A DIE FACE-6 EIGHTH NOTE

'⚅'

7

slide-38
SLIDE 38

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A DIE FACE-6 EIGHTH NOTE

'⚅' '♪'

7

slide-39
SLIDE 39

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 137,994 characters in Unicode 12.1
  • 150 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

LATIN CAPITAL LETTER A DIE FACE-6 EIGHTH NOTE

'⚅' '♪'

7

(Demo)

slide-40
SLIDE 40

Mutation Operations

slide-41
SLIDE 41

Some Objects Can Change

9

[Demo]

slide-42
SLIDE 42

Some Objects Can Change

First example in the course of an object changing state

9

[Demo]

slide-43
SLIDE 43

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

slide-44
SLIDE 44

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

same_person

slide-45
SLIDE 45

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

BABY same_person

slide-46
SLIDE 46

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

BABY same_person Unicode character name

slide-47
SLIDE 47

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👨

GIRL same_person Unicode character name

slide-48
SLIDE 48

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👨

GIRL jessica same_person Unicode character name

slide-49
SLIDE 49

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👪

WOMAN jessica same_person Unicode character name

slide-50
SLIDE 50

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER WOMAN jessica same_person Unicode character name

slide-51
SLIDE 51

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation

slide-52
SLIDE 52

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries

slide-53
SLIDE 53

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries {Demo}

slide-54
SLIDE 54

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

slide-55
SLIDE 55

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4]

slide-56
SLIDE 56

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4

slide-57
SLIDE 57

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four)

slide-58
SLIDE 58

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2

slide-59
SLIDE 59

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0
slide-60
SLIDE 60

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-61
SLIDE 61

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-62
SLIDE 62

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-63
SLIDE 63

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-64
SLIDE 64

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! >>> len(four) 2 def mystery(s): s.pop() s.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-65
SLIDE 65

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! >>> len(four) 2 def mystery(s): s.pop() s.pop() def another_mystery(): four.pop() four.pop()

pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

def mystery(s): s[2:] = []

  • r
slide-66
SLIDE 66

Tuples

(Demo)

slide-67
SLIDE 67

Tuples are Immutable Sequences

12

slide-68
SLIDE 68

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

slide-69
SLIDE 69

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3)

slide-70
SLIDE 70

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze()

slide-71
SLIDE 71

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle

slide-72
SLIDE 72

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3)

slide-73
SLIDE 73

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3]

slide-74
SLIDE 74

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze()

slide-75
SLIDE 75

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle

slide-76
SLIDE 76

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!']

slide-77
SLIDE 77

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Next lecture: ooze can change turtle's binding

slide-78
SLIDE 78

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Next lecture: ooze can change turtle's binding

slide-79
SLIDE 79

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Name change: Next lecture: ooze can change turtle's binding

slide-80
SLIDE 80

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Next lecture: ooze can change turtle's binding

slide-81
SLIDE 81

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 Next lecture: ooze can change turtle's binding

slide-82
SLIDE 82

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 Next lecture: ooze can change turtle's binding

slide-83
SLIDE 83

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 >>> x = 3 Next lecture: ooze can change turtle's binding

slide-84
SLIDE 84

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 >>> x = 3 6 Next lecture: ooze can change turtle's binding

slide-85
SLIDE 85

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 Next lecture: ooze can change turtle's binding

slide-86
SLIDE 86

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-87
SLIDE 87

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-88
SLIDE 88

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-89
SLIDE 89

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-90
SLIDE 90

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-91
SLIDE 91

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-92
SLIDE 92

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-93
SLIDE 93

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-94
SLIDE 94

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-95
SLIDE 95

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-96
SLIDE 96

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-97
SLIDE 97

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> s >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-98
SLIDE 98

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> s ([4, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-99
SLIDE 99

Mutation

slide-100
SLIDE 100

Sameness and Change

14

slide-101
SLIDE 101

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces

14

slide-102
SLIDE 102

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator

14

slide-103
SLIDE 103

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change

14

slide-104
SLIDE 104

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed

14

slide-105
SLIDE 105

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

slide-106
SLIDE 106

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10]

slide-107
SLIDE 107

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a

slide-108
SLIDE 108

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True

slide-109
SLIDE 109

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20)

slide-110
SLIDE 110

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20]

slide-111
SLIDE 111

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20]

slide-112
SLIDE 112

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-113
SLIDE 113

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-114
SLIDE 114

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-115
SLIDE 115

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-116
SLIDE 116

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-117
SLIDE 117

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-118
SLIDE 118

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-119
SLIDE 119

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> b [10, 20] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-120
SLIDE 120

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> b [10, 20] >>> a == b False >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a [10, 20] >>> b [10, 20] >>> a == b True

slide-121
SLIDE 121

Identity Operators

15

slide-122
SLIDE 122

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object

15

slide-123
SLIDE 123

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values

15

slide-124
SLIDE 124

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values

15

slide-125
SLIDE 125

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values

15

(Demo)

slide-126
SLIDE 126

Mutable Default Arguments are Dangerous

16

slide-127
SLIDE 127

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

slide-128
SLIDE 128

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ...

slide-129
SLIDE 129

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1

slide-130
SLIDE 130

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2

slide-131
SLIDE 131

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3

slide-132
SLIDE 132

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

pythontutor.com/composingprograms.html#code=def%20f%28s%3D[]%29%3A%0A%20%20%20%20s.append%283%29%0A%20%20%20%20return%20len%28s%29%0A%20%20%20%20%0Af%28%29%0Af%28%29%0Af%28%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3

slide-133
SLIDE 133

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

pythontutor.com/composingprograms.html#code=def%20f%28s%3D[]%29%3A%0A%20%20%20%20s.append%283%29%0A%20%20%20%20return%20len%28s%29%0A%20%20%20%20%0Af%28%29%0Af%28%29%0Af%28%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3 Each time the function is called, s is bound to the same value!

slide-134
SLIDE 134

Lists

slide-135
SLIDE 135

Lists in Environment Diagrams

18

slide-136
SLIDE 136

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

slide-137
SLIDE 137

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation

slide-138
SLIDE 138

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example

slide-139
SLIDE 139

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result

slide-140
SLIDE 140

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list

slide-141
SLIDE 141

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0

slide-142
SLIDE 142

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0

Global

slide-143
SLIDE 143

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0

Global s list 1

2

t

3

list 1

5 6

slide-144
SLIDE 144

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0

Global s list 1

2

t

3

list 1

5 6

2

slide-145
SLIDE 145

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0

Global s list 1

2

t

3

list 1

5 6

2

slide-146
SLIDE 146

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0

Global s list 1

2

t

3

list 1

5 6

2

slide-147
SLIDE 147

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0

Global s list 1

2

t

3

list 1

5 6

slide-148
SLIDE 148

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list

Global s list 1

2

t

3

list 1

5 6

slide-149
SLIDE 149

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0

Global s list 1

2

t

3

list 1

5 6

slide-150
SLIDE 150

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0

Global s list 1

2

t

3

list 1

5 6

2 3

5 6

slide-151
SLIDE 151

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0

Global s list 1

2

t

3

list 1

5 6

2 3

5 6

slide-152
SLIDE 152

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0]

Global s list 1

2

t

3

list 1

5 6

2 3

5 6

slide-153
SLIDE 153

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0]

Global s list 1

2

t

3

list 1

5 6

slide-154
SLIDE 154

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements

Global s list 1

2

t

3

list 1

5 6

slide-155
SLIDE 155

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global s list 1

2

t

3

list 1

5 6

slide-156
SLIDE 156

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global s list 1

2

t

3

list 1

5 6

list

slide-157
SLIDE 157

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global s list 1

2

t

3

list 1

5 6

list 1

2 3

2 list

slide-158
SLIDE 158

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a list

slide-159
SLIDE 159

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a

slide-160
SLIDE 160

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global b s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a

slide-161
SLIDE 161

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global b s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a list 1

3

slide-162
SLIDE 162

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global b s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a list 1

3 9

slide-163
SLIDE 163

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0

Global b s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a list 1

3 9

slide-164
SLIDE 164

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

18

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]]

Global b s list 1

2

t

3

list 1

5 6

list 1

2 3

2 a list 1

3 9

slide-165
SLIDE 165

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

19

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0

slide-166
SLIDE 166

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

19

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0

list 1

2 3

slide-167
SLIDE 167

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

19

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0

list 1

2 3

slide-168
SLIDE 168

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

19

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0

list 1

2 3

slide-169
SLIDE 169

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

19

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0

list 1

2 3

s → [2, 0] t → [2, 3]

slide-170
SLIDE 170

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

20

Global s list 1

2

t

3

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0 s → [2, 0] t → [2, 3] slice assignment replaces a slice with new values s[0:0] = t s[3:] = t t[1] = 0

slide-171
SLIDE 171

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

21

Global s list 1

5

t

6

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0 s → [2, 0] t → [2, 3] slice assignment replaces a slice with new values s[0:0] = t s[3:] = t t[1] = 0

3 2

2 3

slide-172
SLIDE 172

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

21

Global s list 1

5

t

6

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0 s → [2, 0] t → [2, 3] slice assignment replaces a slice with new values s[0:0] = t s[3:] = t t[1] = 0

3 2

2 3 5

4

6

slide-173
SLIDE 173

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

21

Global s list 1

5

t

6

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0 s → [2, 0] t → [2, 3] slice assignment replaces a slice with new values s[0:0] = t s[3:] = t t[1] = 0

3 2

2 3 5

4

6

slide-174
SLIDE 174

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

21

Global s list 1

5

t

6

list 1

5 6

Operation Example Result append adds one element to a list s.append(t) t = 0 s → [2, 3, [5, 6]] t → 0 extend adds all elements in one list to another list s.extend(t) t[1] = 0 s → [2, 3, 5, 6] t → [5, 0] addition & slicing create new lists containing existing elements a = s + [t] b = a[1:] a[1] = 9 b[1][1] = 0 s → [2, 3] t → [5, 0] a → [2, 9, [5, 0]] b → [3, [5, 0]] The list function also creates a new list containing existing elements t = list(s) s[1] = 0 s → [2, 0] t → [2, 3] slice assignment replaces a slice with new values s[0:0] = t s[3:] = t t[1] = 0

3 2

2 3 5

4

6

s → [5, 6, 2, 5, 6] t → [5, 0]

slide-175
SLIDE 175

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

slide-176
SLIDE 176

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result

slide-177
SLIDE 177

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element

slide-178
SLIDE 178

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop()

slide-179
SLIDE 179

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3

slide-180
SLIDE 180

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument

slide-181
SLIDE 181

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument t.extend(t) t.remove(5)

slide-182
SLIDE 182

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument t.extend(t) t.remove(5) s → [2, 3] t → [6, 5, 6]

slide-183
SLIDE 183

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument t.extend(t) t.remove(5) s → [2, 3] t → [6, 5, 6] slice assignment can remove elements from a list by assigning [] to a slice.

slide-184
SLIDE 184

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument t.extend(t) t.remove(5) s → [2, 3] t → [6, 5, 6] slice assignment can remove elements from a list by assigning [] to a slice. s[:1] = [] t[0:2] = []

slide-185
SLIDE 185

Lists in Environment Diagrams

Assume that before each example below we execute: s = [2, 3] t = [5, 6]

22

Operation Example Result pop removes & returns the last element removes & returns t = s.pop() s → [2] t → 3 remove removes the first element equal to the argument t.extend(t) t.remove(5) s → [2, 3] t → [6, 5, 6] slice assignment can remove elements from a list by assigning [] to a slice. s[:1] = [] t[0:2] = [] s → [3] t → []

slide-186
SLIDE 186

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

slide-187
SLIDE 187

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

slide-188
SLIDE 188

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

list [t] evaluates to:

slide-189
SLIDE 189

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

list [t] evaluates to:

slide-190
SLIDE 190

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

list [t] evaluates to: 1

slide-191
SLIDE 191

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1

slide-192
SLIDE 192

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3

slide-193
SLIDE 193

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3

[1, [...], 1, [...]]

slide-194
SLIDE 194

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3 Global t list 1 list 1

1 2

list 1

3 4

[1, [...], 1, [...]]

slide-195
SLIDE 195

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3 Global t list 1 list 1

1 2

list 1

3 4

list

[1, [...], 1, [...]]

slide-196
SLIDE 196

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3 Global t list 1 list 1

1 2

list 1

3 4

list 2

[1, [...], 1, [...]]

slide-197
SLIDE 197

Lists in Lists in Lists in Environment Diagrams

t = [1, 2, 3] t[1:3] = [t] t.extend(t)

23

t = [[1, 2], [3, 4]] t[0].append(t[1:2])

Global t list 1

1 2

2

3

1 2

1

3 Global t list 1 list 1

1 2

list 1

3 4

list 2

[1, [...], 1, [...]] [[1, 2, [[3, 4]]], [3, 4]]