โ† All PCEP Flashcard Decks

Variable Scope and the `global` keyword Flashcards

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

Read the first 7 Variable Scope and the `global` keyword flashcards as text
  1. What is the output of the following code? ```python var = 100 def my_func(): var = 50 print(var) my_func() print(var) ```

    Answer: 50 100

    Inside `my_func`, `var = 50` creates a new local variable named `var` that shadows the global variable. This local variable is printed (50). The global variable `var` remains unchanged, so the final `print(var)` statement prints its original value (100).

  2. What is the output of this code snippet? ```python var = 100 def my_func(): global var var = 50 my_func() print(var) ```

    Answer: 50

    The `global var` statement tells Python that within this function, `var` refers to the globally scoped variable. Therefore, the assignment `var = 50` modifies the global variable, not a local one. The final print statement outputs the new value of the global variable.

  3. What is the scope of a variable defined inside a function, without using the `global` keyword?

    Answer: Local scope

    A variable that is created (assigned a value) inside a function is local to that function. It can only be accessed from within that function and is destroyed when the function finishes executing.

  4. What is the output of the following code? ```python value = 1 def change_value(): print(value) change_value() ```

    Answer: 1

    Functions can read or access global variables without needing the `global` keyword. The `global` keyword is only required when you need to modify or assign a new value to the global variable from within the function.

  5. What happens when this code is executed? ```python def my_func(): global new_var new_var = 'Hello' my_func() print(new_var) ```

    Answer: The code prints 'Hello'.

    If you use the `global` keyword for a variable that does not yet exist in the global scope, Python will create that variable in the global scope when it is assigned a value inside the function. Therefore, `new_var` becomes a global variable accessible after the function call.

  6. What is the result of running this code? ```python a = 10 def func(a): a = 5 return a print(func(a)) print(a) ```

    Answer: 5 10

    The `a` in `func(a)` is a function parameter, which is a local variable. When `func(a)` is called, the value of the global `a` (10) is passed to the local `a`. Inside the function, this local `a` is changed to 5 and returned. The global `a` is never modified.

  7. What kind of error will this code produce? ```python count = 0 def increment(): count = count + 1 increment() ```

    Answer: UnboundLocalError

    This code causes an `UnboundLocalError`. When you assign to a variable in a scope (e.g., `count = ...`), Python treats it as a local variable for the entire scope. Therefore, when it tries to read `count` on the right side of the expression, it sees a local variable that hasn't been assigned a value yet.