1. Home
  2. Explore
  3. Computer Science
  4. Essential Python Concepts

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Essential Python Concepts

  • 33Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Mutability and Side Effects

What is the difference between a mutable and an immutable object in Python?

33 questions

  1. 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. 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. 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. 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 = x does not make a separate copy of a list; both names refer to the same object.

  5. 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. 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. 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. 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 000 through 999.

    Explanation

    The expression before for supplies each value placed in the list.

  9. 9

    In a list comprehension, what role does the expression before the for clause 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 expression i supplies each list element.

  10. 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 000 through 888.

    Explanation

    The if clause includes an iteration's value only when its condition is true; range(10) stops before 101010.

  11. 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. 12

    What does [[j for j in range(5)] for i in range(10)] produce?

    Model answer

    It produces a list containing 101010 inner lists, each containing the integers from 000 through 444.

    Explanation

    The outer comprehension runs 101010 times, and each inner comprehension creates a five-item list.

  13. 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. 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. 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. 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 x and y, a call with 1, 2 assigns 1 to x and 2 to y.

  17. 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. 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. 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 10 has that value when no argument is supplied for it.

  20. 20

    What does *args allow 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. 21

    What does **kwargs allow 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.

1 of 33
Question 1 of 33

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent