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

  • 41Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Mutability
A Python object that cannot be changed after it is created is called
.

41 questions

  1. 1
    A Python object that cannot be changed after it is created is called
    _____
    .
    Hint

    Think of the opposite of an object that can be modified in place.

    Answer

    immutable

    Explanation

    Immutable objects cannot be changed after creation; assigning a new value to a variable does not modify the original object.

  2. 2
    A Python object whose contents can be modified after it is created is called
    _____
    .
    Hint

    Lists are an example of this kind of object.

    Answer

    mutable

    Explanation

    Mutable objects can be modified after creation, including through a reference held by another variable.

  3. 3
    Which built-in sequence type is immutable: list or tuple?
    _____
    Hint

    This type uses parentheses in its common literal form.

    Answer

    tuple

    Explanation

    A tuple is immutable, so its items cannot be reassigned after the tuple is created.

  4. 4
    Which built-in sequence type allows items to be changed in place: list or tuple?
    _____
    Hint

    This type is often used when a collection needs to grow or change.

    Answer

    list

    Explanation

    A list is mutable, so its items can be changed in place.

  5. 5
    Which built-in mapping type is mutable and stores key-value pairs?
    _____
    Hint

    It is commonly used to look up a value by a key.

    Answer

    dictionary

    Explanation

    A dictionary is mutable, so its contents can be added, removed, or changed.

  6. 6
    Which built-in collection type is mutable and stores unique elements?
    _____
    Hint

    This collection is useful when duplicate elements are not wanted.

    Answer

    set

    Explanation

    A set is mutable, so elements can be added or removed after it is created.

  7. 7
    Which group contains only immutable built-in types: strings, integers, floats, booleans, bytes, and tuples, or lists, sets, and dictionaries?
    _____
    Hint

    Choose the group whose members cannot be modified in place.

    Answer

    strings, integers, floats, booleans, bytes, and tuples

    Explanation

    Strings, integers, floats, booleans, bytes, and tuples are immutable types in Python.

  8. 8
    If two variables refer to the same list, changing an item through one variable also changes what is seen through the other because both refer to the same
    _____
    .
    Hint

    The variables act as two names pointing to one underlying thing.

    Answer

    object

    Explanation

    Assigning a mutable object such as a list to another variable creates an additional reference to that same object, not a separate list.

  9. 9
    If a variable holding an immutable value is reassigned, the other variable previously assigned that value is not changed, because the reassignment gives the first variable a new
    _____
    .
    Hint

    Reassignment changes what one variable is bound to, rather than modifying the old immutable object.

    Answer

    value

    Explanation

    With immutable objects, rebinding one variable does not alter the value associated with another variable.

  10. 10
    A function that sorts a passed-in list using its in-place sort operation can change the caller's list; this modification is a
    _____
    effect.
    Hint

    The phrase describes an unintended or additional change caused by a function.

    Answer

    side

    Explanation

    Sorting a list in place mutates the same object the caller passed, which is a side effect. Programmers should decide intentionally whether a function should mutate its inputs.

  11. 11
    A list comprehension is a compact way to create a list by placing a
    _____
    inside square brackets.
    Hint

    It iterates over values to generate the list's contents.

    Answer

    for loop

    Explanation

    A list comprehension uses a compact loop-like structure to produce a new list.

  12. 12
    In a list comprehension, the expression before the for clause specifies the
    _____
    to place in the resulting list.
    Hint

    It determines what each iteration contributes to the new list.

    Answer

    value

    Explanation

    The expression at the beginning of a list comprehension is evaluated for each iteration and its result is added to the list.

  13. 13
    The comprehension [i for i in range(10)] produces integers from 0 through
    _____
    .
    Hint

    The stop value in range is excluded.

    Answer

    9

    Explanation

    The range function starts at 0 by default and excludes its stop value, so range(10) produces 0 through 9.

  14. 14
    A list comprehension can include another comprehension as its element expression to create a
    _____
    list.
    Hint

    The resulting structure contains lists inside a list.

    Answer

    nested

    Explanation

    A nested list comprehension can build a list whose elements are themselves lists.

  15. 15
    In a list comprehension, an if clause includes a value only when its
    _____
    evaluates to true.
    Hint

    This part tests whether a generated value should be kept.

    Answer

    condition

    Explanation

    A conditional clause filters the values generated by a comprehension, retaining only those for which the condition is true.

  16. 16
    A comprehension that selects values for which i % 2 == 0 keeps only
    _____
    numbers.
    Hint

    These integers are divisible by two with no remainder.

    Answer

    even

    Explanation

    A remainder of zero when dividing by two identifies an even number. With range(10), the retained values are 0, 2, 4, 6, and 8.

  17. 17
    In a function definition, the names listed in the function's parentheses are called
    _____
    .
    Hint

    These names act as placeholders for values the function can receive.

    Answer

    parameters

    Explanation

    Parameters are the names declared in a function definition to receive values.

  18. 18
    The values supplied when calling a function are called
    _____
    .
    Hint

    These are the actual values passed into the function.

    Answer

    arguments

    Explanation

    Arguments are the values provided to a function call; they are assigned to the function's parameters.

  19. 19
    When values are assigned to function parameters according to their position in the call, they are passed as
    _____
    arguments.
    Hint

    The first supplied value goes to the first parameter, and so on.

    Answer

    positional

    Explanation

    Positional arguments are matched to parameters based on their order in the function call.

  20. 20
    When a function call explicitly names a parameter, such as y=2, the value is passed as a
    _____
    argument.
    Hint

    This style identifies the destination parameter by name.

    Answer

    keyword

    Explanation

    A keyword argument specifies the parameter name along with its value, so it does not rely on position.

  21. 21
    In a function call, positional arguments must appear before
    _____
    arguments.
    Hint

    Python rejects a positional value placed after a named value in the same call.

    Answer

    keyword

    Explanation

    Positional arguments must come before keyword arguments; placing a positional argument after a keyword argument raises an error.

  22. 22
    A parameter declared with a default value, such as z=10, is
    _____
    when calling the function.
    Hint

    The caller may omit it and let the default be used.

    Answer

    optional

    Explanation

    A parameter with a default value is optional because the caller does not have to supply an argument for it.

  23. 23
    If an optional parameter is omitted, the function uses its
    _____
    value.
    Hint

    This value is specified in the function definition.

    Answer

    default

    Explanation

    A default value is used whenever the caller omits the corresponding optional parameter.

  24. 24
    A required positional parameter without a supplied value causes a function call to raise an error for a missing
    _____
    argument.
    Hint

    Unlike an optional parameter, this one has no default to fall back on.

    Answer

    required

    Explanation

    A required parameter must receive a value; otherwise, Python reports that a required argument is missing.

  25. 25
    A parameter written as *args collects extra positional arguments into a
    _____
    .
    Hint

    This immutable sequence stores the additional positional values.

    Answer

    tuple

    Explanation

    The *args parameter gathers any extra positional arguments into a tuple, including an empty tuple when none are supplied.

  26. 26
    A parameter written as **kwargs collects extra keyword arguments into a
    _____
    .
    Hint

    The names of the arguments become keys.

    Answer

    dictionary

    Explanation

    The **kwargs parameter gathers extra keyword arguments into a dictionary, where each keyword is a key associated with its value.

  27. 27
    Using one asterisk before a list in a function call unpacks its elements as separate
    _____
    arguments.
    Hint

    Each list element is assigned by its place in the call.

    Answer

    positional

    Explanation

    The * unpacking operator expands an iterable such as a list into individual positional arguments.

  28. 28
    Using two asterisks before a dictionary in a function call unpacks its entries as
    _____
    arguments.
    Hint

    Dictionary keys identify the names of the parameters receiving the values.

    Answer

    keyword

    Explanation

    The ** unpacking operator expands a dictionary into keyword arguments, using its keys as parameter names.

  29. 29
    A function can accept both extra positional and extra keyword arguments by using *args and
    _____
    .
    Hint

    This parameter gathers named arguments into a mapping.

    Answer

    **kwargs

    Explanation

    A function can use *args to collect extra positional arguments and **kwargs to collect extra keyword arguments.

  30. 30
    The special name-and-value test used to check whether a Python file is being run directly is commonly called the
    _____
    guard.
    Hint

    Its name is associated with the main program entry point.

    Answer

    main

    Explanation

    The main guard checks whether the current module is being run as the main program rather than imported by another module.

  31. 31
    When a Python file is imported as a module, its top-level code normally executes during the
    _____
    process.
    Hint

    This is how one module makes another module's functions available.

    Answer

    import

    Explanation

    Importing a module causes Python to read and execute its top-level statements, which is why a main guard is useful.

  32. 32
    Putting startup code inside the main guard prevents that code from running merely because the file is
    _____
    .
    Hint

    The guard distinguishes direct execution from being brought in by another module.

    Answer

    imported

    Explanation

    Code inside the main guard runs when the file is executed directly, but not when it is imported as a module.

  33. 33
    A module can keep reusable utility functions available to other files while reserving program startup actions for
    _____
    execution.
    Hint

    This means running the module itself rather than loading it from another module.

    Answer

    direct

    Explanation

    The main guard allows a module to provide reusable functions while running startup behavior only when the module is executed directly.

  34. 34
    GIL stands for
    _____
    .
    Hint

    It is a lock associated with Python's interpreter.

    Answer

    Global Interpreter Lock

    Explanation

    The Global Interpreter Lock, or GIL, is the interpreter lock discussed in the context of Python threads.

  35. 35
    In the described GIL model, a thread must acquire the interpreter
    _____
    before executing Python code.
    Hint

    This mechanism grants a thread permission to proceed.

    Answer

    lock

    Explanation

    The GIL requires a thread to acquire the interpreter lock before executing Python code.

  36. 36
    Under the GIL behavior described, only
    _____
    thread can execute Python code at a time.
    Hint

    The lock limits simultaneous execution, even on a computer with multiple CPU cores.

    Answer

    one

    Explanation

    The GIL allows only one thread at a time to execute Python code, according to the behavior described in the content.

  37. 37
    A thread is a component of an application that is executed by the
    _____
    .
    Hint

    This hardware component carries out program instructions.

    Answer

    CPU

    Explanation

    A thread is an execution unit within an application that is carried out by the CPU.

  38. 38
    Dividing an application into separate threads so different parts can make progress is called
    _____
    .
    Hint

    The term combines multiple execution paths with threads.

    Answer

    multithreading

    Explanation

    Multithreading organizes different pieces of a program into separate threads so they can make progress as independent tasks.

  39. 39
    Because of the GIL behavior described, adding threads does not provide a CPU-bound Python program with a
    _____
    bonus.
    Hint

    The missing word refers to an improvement in speed or efficiency.

    Answer

    performance

    Explanation

    The content explains that the GIL prevents multiple threads from executing Python code simultaneously, so threading does not speed up the described CPU-bound work.

  40. 40
    Splitting a numeric workload into parts, computing each part, and combining the results is a way to try to
    _____
    work across threads.
    Hint

    This means carrying out separate portions of a task at the same time.

    Answer

    parallelize

    Explanation

    The example divides a sum into sections that could be processed simultaneously on separate cores in a language without the stated GIL limitation.

  41. 41
    Even when a Python program has multiple threads and multiple CPU cores, the GIL limits Python-code execution to one thread at a
    _____
    .
    Hint

    Complete the phrase describing simultaneity: “at a ___.”

    Answer

    time

    Explanation

    The GIL means that, in the behavior described, only one thread can execute Python code at a time, regardless of the number of CPU cores.

1 of 41
Question 1 of 41

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent