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

  • 49Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Mutability
  • 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?

Tap to flipClick to flipspace

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

  1. 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.

  2. Which built-in Python types are immutable, according to the content?

    Strings, integers, floats, booleans, bytes, and tuples.

  3. Which built-in Python types are mutable, according to the content?

    Lists, sets, and dictionaries.

  4. 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.

  5. If y = x and x refers to an immutable value, what happens to y when x is later reassigned?

    Reassigning x does not change the value that y refers to; y remains bound to its original value.

  6. If y = x and x refers to a list, what does the assignment make y refer to?

    y refers to the same list object as x; it is an alias, not a separate list.

  7. What happens to y if an element is changed in a list that both x and y refer to?

    The change is visible through both variables because they refer to the same list object.

  8. What does it mean to sort a list in place?

    The existing list object is reordered, rather than a separate sorted list being created.

  9. 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.

  10. 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.

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

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

  13. In a list comprehension, what does the expression before the for clause determine?

    The value produced for each item added to the new list.

  14. What list is produced by [i for i in range(10)]?

    A list containing the integers from 000 through 999.

  15. How can a list comprehension include a condition that filters which values are added?

    Add an if condition after the iteration clause; only values for which the condition is true are included.

  16. What values are produced by [i for i in range(10) if i % 2 == 0]?

    The even integers from 000 through 888; 101010 is excluded because range(10) stops before 101010.

  17. What is a nested list comprehension?

    A list comprehension placed inside the expression of another list comprehension, often used to build nested lists.

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

    A list containing 101010 inner lists, each containing the integers from 000 through 444.

  19. 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.

  20. 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.

  21. In a function call, what is an argument?

    A value supplied to a function when it is called.

  22. 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.

  23. 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.

  24. Can a function call use positional arguments and keyword arguments together?

    Yes. Positional arguments must come before keyword arguments in the call.

  25. 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.

  26. How is a function parameter made optional?

    Give it a default value in the function definition, such as z=10.

  27. What value does an optional parameter receive when its argument is omitted?

    It receives the default value specified in the function definition.

  28. What happens if a required parameter is omitted from a function call?

    Python raises an error indicating that a required argument is missing.

  29. What does *args allow a function to accept?

    Any number of additional positional arguments; Python collects them into a tuple.

  30. What does args contain when a function with *args receives no extra positional arguments?

    An empty tuple.

  31. What does **kwargs allow a function to accept?

    Any number of keyword arguments; Python collects them into a dictionary.

  32. How can a function access a keyword argument collected in kwargs?

    Use its key, for example kwargs['x'] to access the value for the keyword x.

  33. Can a function accept both *args and **kwargs?

    Yes. It can collect extra positional arguments in a tuple and extra keyword arguments in a dictionary.

  34. What does * do when used before a list in a function call?

    It unpacks the list into separate positional arguments.

  35. 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.

  36. 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.

  37. What is the value of __name__ when a Python file is run directly?

    It is '__main__'.

  38. 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__'.

  39. 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.

  40. 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.

  41. 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.

  42. What does GIL stand for?

    Global Interpreter Lock.

  43. 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.

  44. 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.

  45. 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.

  46. 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.

  47. 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.

  48. 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.

  49. 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.

Card 1 of 49
Card 1 of 49

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent