Now that we understand that everything is an object and have a little bit of knowledge, let’s pause and look a little bit closer at how Python works with different types of objects.
BTW, have you ever modified a variable without knowing it or wanting to? I mean:
>>> a = 1
>>> b = a
>>> a = 2
>>> b
1
>>>
OK. But what about this?
>>> l = [1, 2, 3]
>>> m = l
>>> l[0] = 'x'
>>> m
['x', 2, 3]
>>>
This project is a little bit different than the usual projects. The first part is only questions about Python’s specificity like “What would be the result of…”. You should read all documentation first (as usual :)), then take the time to think and brainstorm with your peers about what you think and why. Try to do this without coding anything for a few hours.
Trying examples in the Python interpreter will give you most of the answers without having to think about it. Don’t go this route. First read, then think, then brainstorm together. Only then you can test in the interpreter.
It’s important that you truly understand the reasons behind the answers of all those tasks so that you can apply the same logic to other variables and other variable types.
Note that during interviews for Python positions, you will most likely have to answer to these type of questions.
All your answers should be only one line in a file. No space before or after the answer.
Read or watch:
- 9.10. Objects and values
- 9.11. Aliasing
- Immutable vs mutable types
- Mutation (Only this chapter)
- 9.12. Cloning lists
- Python tuples: immutable but potentially changing
- Allowed editors:
vi
,vim
,emacs
- All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.8.5)
- All your files should end with a new line
- The first line of all your files should be exactly
#!/usr/bin/python3
- A
README.md
file, at the root of the folder of the project, is mandatory - Your code should use the pycodestyle (version
2.8.*
) - All your files must be executable
- The length of your files will be tested using
wc
- Only one line
- No Shebang
- All your files should end with a new line
What function would you use to print the type of an object?
Write the name of the function in the file, without ()
.
How do you get the variable identifier (which is the memory address in the CPython implementation)?
Write the name of the function in the file, without ()
.
In the following code, do a
and b
point to the same object? Answer with Yes
or No
.
>>> a = 89
>>> b = 100
In the following code, do a
and b
point to the same object? Answer with Yes
or No
.
>>> a = 89
>>> b = 89
In the following code, do a
and b
point to the same object? Answer with Yes
or No
.
>>> a = 89
>>> b = a
In the following code, do a
and b
point to the same object? Answer with Yes
or No
.
>>> a = 89
>>> b = a + 1
What do these 3 lines print?
>>> s1 = "Best School"
>>> s2 = s1
>>> print(s1 == s2)
What do these 3 lines print?
>>> s1 = "Best"
>>> s2 = s1
>>> print(s1 is s2)
What do these 3 lines print?
>>> s1 = "Best School"
>>> s2 = "Best School"
>>> print(s1 == s2)
What do these 3 lines print?
>>> s1 = "Best School"
>>> s2 = "Best School"
>>> print(s1 is s2)
10. And with a list, is it equal
What do these 3 lines print?
>>> l1 = [1, 2, 3]
>>> l2 = [1, 2, 3]
>>> print(l1 == l2)
11. And with a list, is it the same
What do these 3 lines print?
>>> l1 = [1, 2, 3]
>>> l2 = [1, 2, 3]
>>> print(l1 is l2)
12. And with a list, is it really equal
What do these 3 lines print?
>>> l1 = [1, 2, 3]
>>> l2 = l1
>>> print(l1 == l2)
13. And with a list, is it really the same
What do these 3 lines print?
>>> l1 = [1, 2, 3]
>>> l2 = l1
>>> print(l1 is l2)
What does this script print?
l1 = [1, 2, 3]
l2 = l1
l1.append(4)
print(l2)
What does this script print?
l1 = [1, 2, 3]
l2 = l1
l1 = l1 + [4]
print(l2)
What does this script print?
def increment(n):
n += 1
a = 1
increment(a)
print(a)
What does this script print?
def increment(n):
n.append(4)
l = [1, 2, 3]
increment(l)
print(l)
What does this script print?
def assign_value(n, v):
n = v
l1 = [1, 2, 3]
l2 = [4, 5, 6]
assign_value(l1, l2)
print(l1)
Write a function def copy_list(l):
that returns a copy of a list.
- The input list can contain any type of objects
- Your file should be maximum 3-line long (no documentation needed)
- You are not allowed to import any module
guillaume@ubuntu:~/0x09$ cat 19-main.py
#!/usr/bin/python3
copy_list = __import__('19-copy_list').copy_list
my_list = [1, 2, 3]
print(my_list)
new_list = copy_list(my_list)
print(my_list)
print(new_list)
print(new_list == my_list)
print(new_list is my_list)
guillaume@ubuntu:~/0x09$ ./19-main.py
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
True
False
guillaume@ubuntu:~/0x09$ wc -l 19-copy_list.py
3 19-copy_list.py
guillaume@ubuntu:~/0x09$
Is a
a tuple? Answer with Yes
or No
.
a = ()