Essential Python Concepts
- 49Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is the difference between a mutable and an immutable object in Python?
A mutable object can be changed after it is created; an immutable object cannot be changed after it is created.
Or swipe the card
49 cards
What is the difference between a mutable and an immutable object in Python?
A mutable object can be changed after it is created; an immutable object cannot be changed after it is created.
Which built-in Python types are immutable, according to the content?
Strings, integers, floats, booleans, bytes, and tuples.
Which built-in Python types are mutable, according to the content?
Lists, sets, and dictionaries.
What happens if you try to assign a new value to an element of a tuple?
Python raises an error because tuples do not support item assignment.
If
y = xandxrefers to an immutable value, what happens toywhenxis later reassigned?Reassigning
xdoes not change the value thatyrefers to;yremains bound to its original value.If
y = xandxrefers to a list, what does the assignment makeyrefer to?yrefers to the same list object asx; it is an alias, not a separate list.What happens to
yif an element is changed in a list that bothxandyrefer to?The change is visible through both variables because they refer to the same list object.
What does it mean to sort a list in place?
The existing list object is reordered, rather than a separate sorted list being created.
Why can a function that sorts a list parameter change the caller’s list?
The parameter refers to the same mutable list object passed by the caller, so sorting it in place affects that object.
What is a side effect in the context of a function that receives a mutable object?
A side effect occurs when the function modifies an object supplied as an argument, changing it beyond the function’s local computation.
What should a programmer consider when a function may modify a mutable argument?
Whether that side effect is intended; code should be written with the possibility of changing the caller’s object in mind.
What is a Python list comprehension?
A compact way to create a list by placing an expression and a loop-like iteration inside square brackets.
In a list comprehension, what does the expression before the
forclause determine?The value produced for each item added to the new list.
What list is produced by
[i for i in range(10)]?A list containing the integers from 0 through 9.
How can a list comprehension include a condition that filters which values are added?
Add an
ifcondition after the iteration clause; only values for which the condition is true are included.What values are produced by
[i for i in range(10) if i % 2 == 0]?The even integers from 0 through 8; 10 is excluded because
range(10)stops before 10.What is a nested list comprehension?
A list comprehension placed inside the expression of another list comprehension, often used to build nested lists.
What does
[[j for j in range(5)] for i in range(10)]produce?A list containing 10 inner lists, each containing the integers from 0 through 4.
Why is it useful to understand list comprehensions in production code?
They are common in Python code, and understanding them helps you read code that uses them—even when the comprehension is complex.
In a function definition, what is a parameter?
A named variable listed in the function definition that receives a value when the function is called.
In a function call, what is an argument?
A value supplied to a function when it is called.
How are positional arguments assigned to parameters?
They are assigned in order: the first positional argument goes to the first parameter, the second to the second, and so on.
What is a keyword argument?
An argument passed by naming its parameter, such as
y=2; this lets you specify which parameter receives the value.Can a function call use positional arguments and keyword arguments together?
Yes. Positional arguments must come before keyword arguments in the call.
What error results from placing a positional argument after a keyword argument in a function call?
Python reports that a positional argument follows a keyword argument.
How is a function parameter made optional?
Give it a default value in the function definition, such as
z=10.What value does an optional parameter receive when its argument is omitted?
It receives the default value specified in the function definition.
What happens if a required parameter is omitted from a function call?
Python raises an error indicating that a required argument is missing.
What does
*argsallow a function to accept?Any number of additional positional arguments; Python collects them into a tuple.
What does
argscontain when a function with*argsreceives no extra positional arguments?An empty tuple.
What does
**kwargsallow a function to accept?Any number of keyword arguments; Python collects them into a dictionary.
How can a function access a keyword argument collected in
kwargs?Use its key, for example
kwargs['x']to access the value for the keywordx.Can a function accept both
*argsand**kwargs?Yes. It can collect extra positional arguments in a tuple and extra keyword arguments in a dictionary.
What does
*do when used before a list in a function call?It unpacks the list into separate positional arguments.
What does
**do when used before a dictionary in a function call?It unpacks the dictionary into keyword arguments, using its keys as parameter names.
What does the condition
if __name__ == '__main__':indicate?It checks whether the current Python file is being run directly rather than imported as a module.
What is the value of
__name__when a Python file is run directly?It is
'__main__'.What happens to code inside the main guard when the file is imported?
That code does not run as part of the import, because the imported file’s
__name__is not'__main__'.Why put executable code inside an
if __name__ == '__main__':block?To run that code when the file is executed directly, but not when the file is imported by another module.
What is a common use for code placed outside a main guard in a module?
Definitions such as utility functions that other files can import and use.
What kinds of actions might be placed inside a main guard?
Actions intended to happen only when the file is run directly, such as starting a program or initializing a game.
What does GIL stand for?
Global Interpreter Lock.
What does the Global Interpreter Lock require of Python threads, as described in the content?
A thread must acquire the interpreter lock to execute Python code.
What is the main effect of the GIL on Python threads?
Only one thread can execute Python code at a time, even when the computer has multiple CPU cores.
What is a thread in an application?
A component of an application that is executed by the CPU; an application can divide work among multiple threads.
Why can multiple CPU cores improve performance in programs that can execute threads in parallel?
Different cores can execute separate operations at the same time, allowing work to be divided across cores.
Why does adding threads not provide the described CPU-bound speedup in Python?
Because the GIL allows only one thread to execute Python code at a time, so threads cannot run those computations simultaneously across multiple cores.
How could a summation task be divided among four threads in a language that supports parallel execution?
Each thread could sum a separate section of the numbers, and the partial sums could then be added together.
How does the content contrast Python’s GIL with threading in a language that permits parallel execution?
In a language that permits parallel execution, separate threads can run on separate CPU cores and speed up divided work; the GIL prevents that kind of simultaneous Python-code execution.

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