|
|
|
Questions
|
|
|
|
|
Don't wait any longer to start homework 3
|
|
|
|
|
Tuples
|
|
|
|
|
list: sequence of arbitrary elements, elements can be added, removed, overwritten (mutable)
|
|
|
|
|
string: sequence of characters (single letters or symbols), elements are fixed (immutable)
|
|
|
|
|
tuple: sequence of arbitrary elements, elements are fixed (immutable)
|
|
|
|
|
a_list = ["a", "b", "c"] a_str = "abc" a_tuple = ("a", "b", "c")
|
|
|
|
|
tuples: can vs can't do
|
|
|
|
|
Why?
|
|
|
|
|
efficiency (technical reasons are beyond the scope of this course)
|
|
|
|
|
safety
|
|
|
|
|
for example, if you are representing locations in latitude and longitude, this data should always come in pairs
|
|
|
|
|
Scope
|
|
|
|
|
Aliasing
|
|
|
|
|
Mystery
|
|
|
|
|
vec = [5,5,5] q = vec q[1] = 7 print(vec)
|
|
|
|
|
vec = [5, 5, 5] q = vec vec = [1, 2, 3] print(q)
|
|
|
|
|
def zero(vec): for i in range(len(vec)): vec[i] = 0 vec = [1,2,3] print(zero(vec))
|
|
|
|
|
looking at these examples with pythontutor.com, we can see that the immediate value of a list variable in memory is an arrow to the actual list
|
|
|
|
|
when we assign a new variable to a list or give a list as input to a function, it's this arrow that is used
|
|
|
|
|
alias.py
|
|
|
|
|
name = "Aaron" first_name = "Aaron"
print(id(name)) print(id(first_name)) print(id("Aaron"))
name = name + "!" print(name, id(name)) print(first_name, id(first_name))
print("mutable example:") ys = [1, 2, 3] xs = [1, 2, 3] print("ys", id(ys)) print("xs", id(xs)) ys.append(5) print("ys", ys, id(ys)) print("xs", xs) zs = ys print("zs", id(zs))
def append10(nums): print("nums", id(nums)) nums += [10]
append10(ys) print("ys", ys, id(ys))
def add10(num): print("num", num, id(num)) num += 10 print("num", num, id(num))
a = 5 print("a", a, id(a)) add10(a) print("a", a, id(a))
|
|
|