V Questions
* Don't wait any longer to start homework 3
V 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)
V 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
V Why?
* efficiency (technical reasons are beyond the scope of this course)
V safety
* for example, if you are representing locations in latitude and longitude, this data should always come in pairs
V Scope
V Mystery
V
x = 5
def square(x):
x = x * x
return x
y = 10
print(square(y))
print(y)
print(x)
* what if we remove x=5
* the scope of a variable refers to the part of a program where that variable exists
V Practice
* def cube(x):
x2 = x * x
x = x2 * x
return x
for num in range(4):
if x % 2 == 0:
x = cube(num)
print(x)
print(num)
print(x)
print(x2)
V Aliasing
V 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))
V 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
V 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))