Essential Python Concepts
- 33Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the difference between a mutable and an immutable object in Python?
33 questions
- 1
What is the difference between a mutable and an immutable object in Python?
Model answer
A mutable object can be changed after it is created, while an immutable object cannot be changed in place.
Explanation
The distinction matters when modifying objects or passing them between variables and functions.
- 2
Name the built-in immutable types and mutable types identified in the content.
Model answer
Immutable types include strings, integers, floats, Booleans, bytes, and tuples. Mutable types include lists, sets, and dictionaries.
Explanation
The content also notes that types from third-party libraries may be mutable.
- 3
Why does assigning a tuple to another variable and then reassigning the first variable not change the second variable's value?
Model answer
Reassigning the first variable makes it refer to a different value; it does not modify the original tuple or the value held by the second variable.
Explanation
A tuple cannot be changed in place, so an attempt to assign to one of its items raises an error.
- 4
What happens when two variables refer to the same list and an item in that list is modified?
Model answer
Both variables reflect the change because they refer to the same mutable list object.
Explanation
An assignment such as
y = xdoes not make a separate copy of a list; both names refer to the same object. - 5
How can a function that sorts a list parameter change a list outside the function?
Model answer
If the function sorts the passed-in list in place, it modifies the same list object that the caller refers to.
Explanation
The parameter refers to the mutable list passed by the caller, so an in-place operation changes that list.
- 6
What is a side effect in the context of a function that accepts a mutable object?
Model answer
A side effect occurs when the function changes a mutable object passed to it, thereby affecting that object outside the function.
Explanation
The content emphasizes that programmers should be deliberate about whether a function should modify its inputs.
- 7
A function receives a list and sorts it in place. What should you consider when deciding whether this behavior is appropriate?
Model answer
Consider whether callers are meant to have their original list modified; if not, the function should avoid mutating that input.
Explanation
In-place sorting is a side effect because it changes the caller's mutable list.
- 8
What is a list comprehension, and what does
[i for i in range(10)]produce?Model answer
A list comprehension constructs a list using an expression and an iteration. This one produces the integers from 0 through 9.
Explanation
The expression before
forsupplies each value placed in the list. - 9
In a list comprehension, what role does the expression before the
forclause play?Model answer
It specifies the value to place in the resulting list for each iteration.
Explanation
For example, in
[i for i in range(10)], the expressionisupplies each list element. - 10
What kind of values does
[i for i in range(10) if i % 2 == 0]include?Model answer
It includes the even integers from 0 through 8.
Explanation
The
ifclause includes an iteration's value only when its condition is true;range(10)stops before 10. - 11
How can a list comprehension create a nested list?
Model answer
Place another list comprehension in the expression of an outer list comprehension.
Explanation
Each iteration of the outer comprehension can produce an entire inner list.
- 12
What does
[[j for j in range(5)] for i in range(10)]produce?Model answer
It produces a list containing 10 inner lists, each containing the integers from 0 through 4.
Explanation
The outer comprehension runs 10 times, and each inner comprehension creates a five-item list.
- 13
Why is it useful to understand list comprehensions even when a comprehension seems complicated?
Model answer
They appear frequently in Python code, so understanding them helps you read and reproduce code that uses them.
Explanation
Comprehensions can simplify a line of code, though complicated ones can sometimes make code harder to understand.
- 14
How do required positional parameters differ from optional parameters with default values?
Model answer
A required parameter must receive a value when the function is called; an optional parameter can be omitted and then uses its default value.
Explanation
Calling a function without a required parameter causes an error, while omitting a parameter with a default is allowed.
- 15
What is the difference between a parameter and an argument?
Model answer
A parameter is a name in a function definition; an argument is a value supplied when calling the function.
Explanation
The content uses “parameter” for the function definition and “argument” for the function call.
- 16
How do positional arguments assign values to parameters?
Model answer
They assign values according to the order in which the parameters appear in the function definition.
Explanation
For parameters
xandy, a call with1, 2assigns1toxand2toy. - 17
How does a keyword argument assign a value to a parameter?
Model answer
It names the parameter and assigns a value to it, such as
y=2, so the value need not be assigned by position.Explanation
Keyword arguments can make calls clearer and allow arguments to be supplied by parameter name.
- 18
When a function call uses both positional and keyword arguments, what order must they follow?
Model answer
Positional arguments must come before keyword arguments.
Explanation
Putting a positional argument after a keyword argument causes an error.
- 19
What does giving a parameter a default value accomplish?
Model answer
It makes the parameter optional: if the caller omits it, the function uses the default value.
Explanation
For example, a parameter declared with a default of
10has that value when no argument is supplied for it. - 20
What does
*argsallow a function to accept, and how are those extra arguments collected?Model answer
It allows the function to accept any number of additional positional arguments, which are collected in a tuple.
Explanation
The function can receive zero or more such arguments after any explicitly declared positional parameters.
- 21
What does
**kwargsallow a function to accept, and how are those extra arguments collected?Model answer
It allows the function to accept any number of additional keyword arguments, which are collected in a dictionary.
Explanation
The keyword names become dictionary keys, and their supplied values become the corresponding values.
- 22
How can a list be unpacked into positional arguments in a function call?
Model answer
Put one asterisk before the list when calling the function, as in
function(*values).Explanation
The list's items are passed as separate positional arguments rather than as one list argument.
- 23
How can a dictionary be unpacked into keyword arguments in a function call?
Model answer
Put two asterisks before the dictionary when calling the function, as in
function(**values).Explanation
The dictionary's keys are used as parameter names and its values are passed as their arguments.
- 24
What does the condition
if __name__ == "__main__":determine?Model answer
It checks whether the current Python file is being run directly rather than imported as a module.
Explanation
When the file is run directly, Python sets its
__name__value to"__main__". - 25
Why place code inside an
if __name__ == "__main__":block?Model answer
It ensures that code intended for direct execution runs when the file is run directly but not when the file is imported.
Explanation
This prevents importers from inadvertently triggering actions such as starting a program or sending a request.
- 26
What happens to top-level code in a Python file when that file is imported?
Model answer
Python reads and executes its top-level code, unless the code is protected by a condition that prevents it from running during import.
Explanation
The
__main__guard is used to separate direct-run behavior from code that should be available to importing modules. - 27
A module contains utility functions and a command that starts a program. How can it expose the functions without starting the program when imported?
Model answer
Put the startup command inside an
if __name__ == "__main__":block.Explanation
The functions remain importable, while the guarded command runs only when the module is executed directly.
- 28
What is the Global Interpreter Lock (GIL) in the Python explanation provided?
Model answer
It is an interpreter lock that a thread must acquire to execute, so only one thread executes at a time under the explanation given.
Explanation
The lock limits simultaneous thread execution even when a computer has multiple CPU cores.
- 29
What is a thread in an application?
Model answer
A thread is a component of an application that is executed by the CPU.
Explanation
Applications can divide work into different threads, for example to handle image processing, user input, and network requests.
- 30
Why might a program use multiple threads?
Model answer
It can separate different parts of an application so they can make progress at the same time, such as processing an image while handling user input.
Explanation
The content describes multi-threading as a way to organize concurrent application tasks.
- 31
According to the content, why does creating several Python threads not speed up a CPU-bound calculation across multiple cores?
Model answer
Because the GIL allows only one thread to execute at a time, the threads cannot perform their CPU-bound work simultaneously across cores.
Explanation
Splitting a calculation among threads therefore does not provide the described parallel execution benefit in Python.
- 32
How does the content illustrate the potential benefit of parallel work when summing a range of numbers?
Model answer
It suggests dividing the range into sections, summing each section separately, and then adding the partial sums together.
Explanation
In a language that can run those threads on separate cores, this division could reduce the time for the calculation.
- 33
What is the difference between having multiple CPU cores and being able to execute multiple Python threads simultaneously under the GIL?
Model answer
Multiple cores can execute operations in parallel, but under the GIL only one Python thread executes at a time, as described in the content.
Explanation
The GIL prevents Python threads from using multiple cores for simultaneous execution of the work discussed.

Ask anything
We've read this whole study set, so questions about it get the best answers.