How to declare a global variable in Python

To declare a global variable in Python, you can use the global keyword inside a function to indicate that a variable should be global, rather than local to the function. Here’s an example:To declare a global variable in Python, you can use the global keyword inside a function to indicate that a variable should be global, rather than local to the function. Here’s an example:

x = 10  # global variable

def my_function():
    global x  # declare x as a global variable
    x = 5    # modify the value of the global variable

my_function()
print(x)   # prints 5

In this example, we declare a global variable x outside of the function. Inside the function my_function(), we use the global keyword to indicate that we want to modify the global variable x, rather than create a new local variable. We then modify the value of x to 5. When we call the function and then print the value of x, we see that the value has indeed been changed to 5.

Changing Global Variable inside the Local Scope:

Yes, you can change the value of a global variable from inside a local scope using the global keyword in Python.

Here’s an example:

x = 10  # global variable

def my_function():
    global x  # declare x as a global variable
    x = 5    # modify the value of the global variable inside the function

my_function()
print(x)   # prints 5

In this example, we use the global keyword to declare x as a global variable inside the function my_function(). Then, we modify the value of x to 5. When we call the function and then print the value of x, we see that the value has indeed been changed to 5.

Note that if you don’t declare x as a global variable inside the function using the global keyword, Python will create a new local variable with the same name instead of modifying the global variable.

Global Keyword:

In Python, the global keyword is used inside a function to indicate that a variable is a global variable, rather than a local variable.

Here’s an example:

x = 10  # global variable

def my_function():
    global x  # declare x as a global variable
    x = 5    # modify the value of the global variable inside the function

my_function()
print(x)   # prints 5

In this example, we declare a global variable x outside of the function. Inside the function my_function(), we use the global keyword to indicate that we want to modify the global variable x, rather than create a new local variable. We then modify the value of x to 5. When we call the function and then print the value of x, we see that the value has indeed been changed to 5.

Without the global keyword, if you try to modify a variable that has the same name as a global variable inside a function, Python will create a new local variable instead of modifying the global variable. This means that the changes you make to the variable inside the function will not affect the global variable.

The global keyword can be useful in cases where you want to modify a global variable from within a function, but it should be used with care to avoid unintended side effects.