KnowledgeBoat Logo

Class - 12 CBSE Computer Science — Assertion Reason Type Questions

Assertion (A): The local and global variables declared with the same name in the function are treated in the same manner by the Python interpreter.

Reasoning (R): The variable declared within the function block is treated as local, whereas the variable declared outside the function block will be referred to as a global variable.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Python Functions

3 Likes

Answer

A is false but R is true.

Explanation
When a variable shares the same name both locally within a function and globally outside it, the Python interpreter treats them differently based on the scope where they are accessed. Local variables, declared within a function, are accessible only within that function and don't affect the global variable of the same name. The interpreter prioritizes the local variable over the global one within the function's scope. However, globally declared variables are accessible throughout the program, and their value can be modified or accessed from any function or block.

Answered By

1 Like