What is a 2D list?
A list that contains other lists as its elements.
What keyword defines a function?
def
How many values can a function return?
0 to n
Which data structure is ordered and immutable?
Tuple
This mode in open() will create a new file for writing and overwrite an existing file.
"w" (write mode)
What structure is commonly processed with nested loops?
2-D lists
What is one benefit of using functions?
Reusable: allows you to use a function's logic whenever and wherever needed.
OR
Modular: makes debugging easier, because you fix the error in one specific block of code.
ACCEPT ANY (OTHER) VALID BENEFITS
What's the difference between a parameter and an argument?
A parameter is the variable in the function signature/definition, and an argument is the value you pass to that parameter.
What is the main difference between a list and a tuple?
lists are mutable, tuples are not
This Python method allows you to remove the newline character from the end of a string when reading from a file.
rstrip()
How do you access the element in row 3, column 2 of matrix?
matrix[2][1]
What kind of scope do variables inside functions have?
local
What do you call a parameter with a default value?
an optional parameter
Which set operation combines all elements from two sets a & b, but removes duplicates?
a | b
Explain why you should not rely on Python automatically closing files at program termination.
Because if the program crashes unexpectedly, buffered data may not be written to the file, resulting in data loss.
How can you safely modify a list while looping through it?
By iterating over a copy of the list or building a new list instead; this is why we use temp values.
What value does a function return if there’s no return statement?
None
Which keyword lets an inner function change a variable that belongs to its outer function?
nonlocal
If:
a = {1,2,3,4}
b = {3,4,5,6}
What does print(a & b) return and
what does print(a and b) return?
print(a&b) = {3,4}
print(a and b) = {3,4,5,6}
Which mode in open() creates a file if it doesn’t exist but adds to it if it does?
"a" - append mode
What is the difference between :
for i in range(len(lst))
for item in lst?
The first gives indices
the second gives the elements directly
What is a default parameter value?
A value assigned in the function definition that is used if no argument is provided.
What is the output of this function call?
def greet(name="Student"):
return f"Hello, {name}!"
greet()
Hello Student!
What is the output?
[1, 2, 3, 4]
Describe the differences between .write(), .writelines(), and print(..., file=...) when writing to a file in Python.
.write() writes a single string and does not add a newline automatically.
.writelines() writes a list of strings as-is and no newlines are added unless included in the strings.
print(..., file=...) writes strings with a newline by default and can be redirected to a file using the file parameter