Aliasing and Mutability - CMU School of Computer Science
Aliasing and Mutability
15-110 ? Wednesday 02/19
Learning Goals
? Recognize how aliasing impacts the values held in mutable variables ? Recognize the difference between functions on mutable values that
are destructive vs. non-destructive ? Use 2D lists when reading and writing code to work on data over
multiple dimensions
2
Last Time...
3
List Values Can Be Changed
Unlike the previous types we've worked with, the values in a list can be changed directly, without creating a new list that needs to be assigned to the variable.
We can change a list by setting a list index to a new value, like how we would set a variable to a value.
lst = [ "a", "b", "c" ] lst[1] = "foo" print(lst) # [ "a", "foo", "c" ]
4
Lists are Mutable; Strings are Immutable
We call data types that can be modified after being assigned mutable. Data types that cannot be modified directly are called immutable.
All the other data types we've learned about so far ? integers, floats, Booleans, and strings ? are immutable. In fact, if we try to set a string index to a new character, we'll get an error. We have to set the entire variable equal to a new value if we want to change the string.
s = "abc" s[1] = "z" # TypeError s = s[:1] + "z" + s[2:]
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- 014 introduction to modules numpy notepad
- python basics cmu school of computer science
- aliasing and mutability cmu school of computer science
- binary 2d morphologies of polymer phase separation dataset and python
- 2 1 1 2 2 2 2 3 4 algorithm flowcharts programming caie master
- cmsc 201 fall 2018 lab 09 2d lists department of computer science and
- lecture 16 department of computer science
- 2d array problems
- the point class university of iowa
- emsat achieve computer science python
Related searches
- high school computer science curriculum
- high school computer science projects
- list of computer science topics
- benefits of computer science degree
- history of computer science pdf
- fundamentals of computer science pdf
- benefits of computer science career
- benefits of computer science education
- doctor of computer science salary
- examples of computer science math
- computer science middle school curriculum
- list of computer science journals