E1. To be completed after lesson 3


Exercise 1.1

Describe in words the difference between a mutable and immutable object.

Exercise 1.2

a) Describe in words the difference between the == operator and the is operator.

b) Why should you not use the == operator to compare two floats?

Exercise 1.3

Do this before Exercise 1.4. Without running the code, write what will be printed for the following Python codes.

a)

my_list = [1, 2, 3]
my_list[2] = "four"

print(my_list)

b)

a = 5
b = 2
a = b

print(a, b)

c)

def first_element_103(x):
    x[0] = 103

x = [1, 2, 3]

first_element_103(x)

print(x)

d)

def add_103(x):
    x += 103

x = 4

add_103(x)

print(x)

e)

def add_103(x):
    return x + 103

x = 4

x = add_103(x)

print(x)

f)

def append_103(x):
    x.append(103)

a = [1, 2, 3]
b = a

append_103(b)

print(a)

g)

add_three_numbers = lambda x, y, z: x + y + z

print(add_three_numbers(*(5, 6, 7)))

h)

print([i for i in range(1, 10) if i % 2])

i) As we will learn when we learn about style, you should not write code like this example; it’s overly tricky to read. But it will help see if you understand how list comprehensions work.

print([i if i % 3 == 0 else i**2 for i in range(1, 10) if i % 2])

j) This one is tricky. Don’t worry if you have trouble with this.

def append_103(x=[]):
    x.append(103)

    return x

append_103()
append_103()
append_103()

print(append_103())

Exercise 1.4

Check your work from Exercise 1.3 by running the codes in code cells. Do not change your answers to Exercise 1.3 after doing this.

Exercise 1.5

What is the difference between a key and a value in a dictionary?

Exercise 1.6

Can a dictionary have duplicate keys? Can it have duplicate values?

Exercise 1.7

Write down any questions or points of confusion that you have.

[1]:
def add_103(x):
    x += 103

x = 4

add_103(x)

print(x)
4
[2]:
my_return_value = add_103(4)
[4]:
print(my_return_value)
None
[10]:
def add_103(x):
    print(id(x))

    x += 103

    print(id(x))

    return x

x = 400

x = add_103(x)
140539926695152
140539926694384
[7]:
x = 4
print(id(x))

x = add_103(x)
print(id(x))
4373822048
4373825344
[11]:
def append_103(x):
    x.append(103)

a = [1, 2, 3]
b = a

append_103(b)

print(a)
[1, 2, 3, 103]
[13]:
a is b
[13]:
True
[14]:
add_three_numbers = lambda x, y, z: x + y + z

print(add_three_numbers(*(5, 6, 7)))
18
[17]:
add_three_numbers(*([1], [2], [3]))
[17]:
[1, 2, 3]
[ ]:
def add_three_numbers(x, y, z):
    return x + y + z
[27]:
bool('')
[27]:
False
[1]:
def append_103(x=[]):
    x.append(103)

    return x

append_103([])
append_103([])
append_103([])

print(append_103([]))
[103]
[2]:
x
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/var/folders/tj/4wnqnhc13cl70g85k6w4kbh80000gn/T/ipykernel_55106/32546335.py in <module>
----> 1 x

NameError: name 'x' is not defined
[31]:
x
[31]:
[103, 103, 103, 103]
[1]:
def first_element_103(x):
    x[0] = 103

x = [1, 2, 3]

first_element_103(x)

print(x)
[103, 2, 3]
[2]:
return_value = first_element_103(x)
[4]:
print(return_value)
None
[5]:
return_value is None
[5]:
True
[12]:
def add_103(x):
    return x + 103

x = 400
print(id(x))

x = add_103(x)
print(id(x))

print(x)
140333373286256
140333373286224
503
[13]:
def append_103(x):
    x.append(103)

a = [1, 2, 3]
b = a

append_103(b)

print(a)
[1, 2, 3, 103]
[14]:
a is b
[14]:
True
[15]:
id(a), id(b)
[15]:
(140333359935232, 140333359935232)
[18]:
add_three_numbers = lambda x, y, z: x + y + z

print(add_three_numbers(*(5, 6, 7)))
18
[17]:
def add_three_numbers(x, y, z):
    return x + y + z
[17]:
<function __main__.<lambda>(x, y, z)>
[26]:
bool([])
[26]:
False
[27]:
list(range(1, 10, 2))
[27]:
[1, 3, 5, 7, 9]
[28]:
print([i if i % 3 == 0 else i**2 for i in range(1, 10) if i % 2])
[1, 3, 25, 49, 9]
[1]:
x = 257
id(x)
[1]:
140670955060848
[2]:
x += 9
id(x)
[2]:
140670955059248
[3]:
def first_element_103(x):
    x[0] = 103

x = [1, 2, 3]

first_element_103(x)

print(x)
[103, 2, 3]
[4]:
return_value = first_element_103(x)
[6]:
print(return_value)
None
[10]:
def add_103(x):
    x = x + 103
    print(id(x))

x = 4
print(id(x))

add_103(x)

print(id(x))
4304173664
4304176960
4304173664
[12]:
def add_103(x):
    return x + 103

x = 400
print(id(x))

x = add_103(x)

print(id(x))
140670955058928
140670955058288
[13]:
def append_103(x):
    x.append(103)

a = [1, 2, 3]
b = a

append_103(b)

print(a)
[1, 2, 3, 103]
[14]:
b
[14]:
[1, 2, 3, 103]
[15]:
a is b
[15]:
True
[16]:
add_three_numbers = lambda x, y, z: x + y + z

print(add_three_numbers(*(5, 6, 7)))
18
[ ]:
def add_three_numbers(x, y, z):
    return x, y, z
[17]:
add_three_numbers
[17]:
<function __main__.<lambda>(x, y, z)>
[18]:
add_three_numbers(3200, 50, 2)
[18]:
3252
[28]:
bool(['a', 'list'])
[28]:
True
[20]:
3 % 2
[20]:
1
[29]:
print([i for i in range(1, 10) if i % 2])
[1, 3, 5, 7, 9]
[30]:
def append_103(x=[]):
    x.append(103)

    return x

append_103()
append_103()
append_103()

print(append_103())
[103, 103, 103, 103]
[ ]: