Concepts Reviewed
These questions are optional supplements that ideally help you get prepared for the quizzes ahead of time.(Some of them are also designed for elaboration purposes as you can see from the tag. Don’t worry them if you just want to get prepared for the quizzes) Before working through these practice questions, you should get familiar with the concepts covered in the lessons of the Fifth Week.
Questions
These questions might not be in the same format as quizzes. Don’t hesitate to bring them to the Tutoring section for help.
List Memory
In Python, when you create a variable of type
list
, where does it get stored? (Hint: Stack or Heap?)In the Memory Diagram, how do we represent something that is stored in the Heap? (Instead of drawing an Arrow, how do we represent the reference to Heap?)
When we see the code like
x: list[int] = [1, 2, 3]
, how do you handle that in Memory Diagram? Will you write anything in Stack?Is
list
in Python fixed length or infinite? (For elaboration: Do you think thelist
in Python is infinitely long? What will happen eventually?)
Import
What is the purpose of having
import
statements in Python? When will we use it?If we import a file say
fileA.py
tofileB.py
do you think the functions fromfileA.py
will get executed after being imported?(Continue on Question 2) You might have noticed that we have a line of code:
if __name__ == "__main__"
. After knowing the answer to previous questions, do you know the purpose we have this line of code?What is the syntax of writing an
import
statement to import a functionhelperA
from a module calledex01
inside the package calledcomp110
?
For Loop
- Use the
range()
function to create afor-in
loop which behaves the same as the following loop:
i: int = 0
while (i < len(list1)):
print(list1[i])
i+=1
Can you think of the reasons why we will prefer the
for-in
loop over thewhile
loop?Fill in the following blank:
str1: str = "I love COMP110"
for _________:
print(char)
Can we nested a loop? If so, could you state one purpose of doing so?
(Elaborate on For-In Loop) Unlike the normal while loop with index, the
for-in
loop might be a little inconvenient to use if we also need to take theindex
into account. Is there a function (beyond 110’s scope) that will record the index while also using thefor-in
loops’ syntax?What would be the output of the following
for-in
loop?
for i in range(5):
result = i * 2
print(result)
- Is the following way of writing
for-in
loop encouraged? Why not or why yes?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number % 2 == 0:
numbers.remove(number)
print(numbers)
- Any problem with the following codes? If not, what is the output?
course: list[str] = ["110", "210", "211", "301"]
for num in course:
print(num + 100)
Solutions
List Memory
The list gets stored in the heap. The variable in the stack holds a reference to the list in the heap.
The reference address associated with the list in the heap.
We first write the name (which is
x
) inside the stack, then we write the address associated with thelist [1, 2, 3]
from the Heap.Unlike the lists from some other languages, Python’s lists are not fixed in length. However, they are not considered infinite as they will be limited by the available memory.
Import
Import statement promote code reusability by importing and using functionality that is defined elsewhere.
(This is an elaborated question) When a file (e.g., fileA.py) is imported into another file (fileB.py), Python executes the top-level code in fileA.py once during the import. However, functions or classes defined in fileA.py are not automatically executed upon import; they must be called explicitly in fileB.py.
This line of code will check if the file is run as the main file or gets imported. Prevent the file from executing unnecessarily.
from comp110.ex01 import helperA
For Loop
for i in range(len(list1)):
print(list1[i])
Readability, Simplicity, or Less Error(Such as no risk of having infinite loop)
Fill in the following blank:
for char in str1:
Yes. Nested loops are useful for working with multi-dimensional data structures, say a matrix.
(Elaborate on For-In Loop) There is a function called
enumerate()
fill free to Google it.It will only print 8
This is not encouraged since if we remove elements from a list while iterating it. Since the index get updated during the iteration lead to the risk that some elements get skipped.
The course is a list of string instead of int. Applying
for-in
loop will return each string from it which cannot be applied withaddition
between a string and numbers.