A Level Computer Science OCR Practice Exam

Question: 1 / 400

Which type of variable can be accessed and modified both inside and outside a function in Python?

Local Variable

Global Variable

A global variable in Python is defined outside any function and can be accessed and modified from anywhere in the code, including within functions. This is particularly useful when you want to maintain a value that should be the same across different parts of your program, without passing it explicitly as a parameter every time.

When inside a function, a global variable can be modified if you declare it as global using the keyword 'global'. This explicit declaration informs Python that you want to refer to the global variable defined outside the function rather than creating a new local variable with the same name.

In contrast, local variables are confined to the function in which they are defined, meaning they cannot be accessed or modified from outside that function. Static and instance variables pertain more to class-based contexts in object-oriented programming, where they serve different scopes and lifetimes, which do not allow the same level of accessibility from outside their respective classes or methods as global variables do.

Get further explanation with Examzify DeepDiveBeta

Static Variable

Instance Variable

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy