hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Which of the following is false about global variables in Python?
    Question

    Which of the following is false about global variables in Python?

    A.

    global immutable variables require global keyword inside function.

    B.

    global mutable variables do not require global keyword inside function.

    C.

    global keyword is a must for the first time of assignment of a global variable.

    D.

    global variables can be accessed anywhere in the program.

    Correct option is A


    The statement "global immutable variables require global keyword inside function" is false. In Python, global immutable variables (like integers, strings, or tuples) do not require the global keyword when accessed inside a function. You only need the global keyword if you intend to modify the global variable inside the function.
    Important Key Points:
    1. Global Variables: Global variables are those defined outside of any function and can be accessed anywhere in the program.
    2. Accessing Global Variables: Global variables can be accessed directly inside functions, but if you intend to modify them, you must use the global keyword.
    3. Immutable Global Variables: For immutable types (like integers, strings, tuples), you do not need the global keyword to access them. However, if you want to reassign them inside a function, you must use global.
    4. Mutable Global Variables: For mutable types (like lists or dictionaries), you can modify them without using the global keyword, but reassigning them would require the global keyword.
    Knowledge Booster:
    · Option (b): This is true. Global mutable variables (such as lists or dictionaries) can be modified inside a function without needing the global keyword. However, reassigning them entirely requires the global keyword.
    · Option (c): This is true. The global keyword is necessary for the first assignment of a global variable inside a function. If you do not use global, Python will create a local variable instead of modifying the global one.
    · Option (d): This is true. Global variables can be accessed from anywhere in the program, inside or outside of functions, as long as they are not shadowed by local variables with the same name.

    Free Tests

    Free
    Must Attempt

    CDP Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    EVS Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    CTET Paper-I PYP (07 July 2024)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    test-prime-package

    Access ‘EMRS PGT’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    289k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow