โ† All Python Flashcard Decks

Data Types and Variables Flashcards

6 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Data Types and Variables flashcards as text
  1. Which of the following data types is immutable in Python?

    Answer: tuple

    In Python, immutable data types are those that cannot be changed after they are created. Tuples, defined with parentheses, are immutable. Once a tuple is created, its elements cannot be altered, added, or removed. Lists, dictionaries, and sets are all mutable, meaning their contents can be changed after creation.

  2. What will be the output of the following Python code? x = 5 y = '10' print(x + int(y))

    Answer: 15

    The code first initializes an integer variable `x` to 5 and a string variable `y` to '10'. The `int()` function is used for type casting, converting the string '10' into the integer 10. Then, the addition operation `x + 10` is performed, which results in `5 + 10 = 15`. The `print()` function then outputs this result.

  3. A programmer is creating a script to store user configurations that should not change during program execution. Which data type is most appropriate for storing a collection of these configuration settings?

    Answer: tuple

    A tuple is the most appropriate choice because it is an immutable data type. This means that once the configuration settings are stored in a tuple, they are protected from accidental modification during the program's execution, ensuring data integrity.

  4. Which of the following is an invalid variable name in Python?

    Answer: 2myVar

    Python variable names must start with a letter (a-z, A-Z) or an underscore (_). They cannot begin with a digit. Therefore, '2myVar' is an invalid variable name. '_my_var', 'myVar2', and 'MYVAR' are all valid.

  5. Consider the following code snippet: def my_func(): x = 10 print(x) x = 20 my_func() print(x)

    Answer: 10 20

    This question tests understanding of variable scope. The `x = 20` assignment creates a global variable. Inside `my_func()`, `x = 10` creates a new local variable `x` that shadows the global one. The `print(x)` inside the function prints the local `x` (10). After the function call, the `print(x)` in the global scope prints the global `x` (20).

  6. What is the data type of the result of the expression `10 / 4`?

    Answer: float

    In Python 3, the standard division operator `/` always performs float division, meaning the result will be a float even if the numbers divide evenly. `10 / 4` results in `2.5`, which is a float data type. Integer division is performed with the `//` operator.