Global and Local Variables in Python

In Python, variables can be defined in two scopes: global and local. Understanding the difference between global and local variables is important for writing effective and readable code.

Global Variables

A global variable is a variable that is defined outside of any function, and can be accessed from anywhere in the program. Global variables can be accessed and modified by any function in the program. Here’s an example:

global_variable = 10

def function():
    print(global_variable)

function()  # Output: 10

In this example, global_variable is defined outside of any function, and can be accessed by the function() function. When function() is called, it prints the value of global_variable, which is 10.

Global variables can also be modified by functions:

global_variable = 10

def function():
    global global_variable
    global_variable = 20

function()
print(global_variable)  # Output: 20

In this example, global_variable is modified inside the function() function. The global keyword is used to indicate that global_variable should refer to the global variable with the same name, rather than creating a new local variable.

Local Variables

A local variable is a variable that is defined inside a function, and can only be accessed and modified within that function. Here’s an example:

def function():
    local_variable = 10
    print(local_variable)

function()  # Output: 10

In this example, local_variable is defined inside the function() function, and can only be accessed within that function. When function() is called, it prints the value of local_variable, which is 10.

Local variables can also have the same name as a global variable, without affecting the value of the global variable:

global_variable = 10

def function():
    global_variable = 20
    print(global_variable)

function()  # Output: 20
print(global_variable)  # Output: 10

In this example, global_variable is defined outside of any function, and is set to 10. Inside the function() function, a new local variable named global_variable is created and set to 20. When function() is called, it prints the value of the local variable global_variable, which is 20. However, when the function finishes executing, the value of the global variable global_variable remains unchanged and is still 10.

In general, it’s a good practice to avoid using global variables, as they can make code more difficult to understand and debug. Instead, local variables should be used whenever possible, to limit the scope of variables and improve code readability.

In summary, global variables are defined outside of any function and can be accessed and modified from anywhere in the program, while local variables are defined inside a function and can only be accessed and modified within that function. The global keyword can be used to modify the value of a global variable inside a function, and local variables can have the same name as global variables without affecting the value of the global variable.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *