Global Variable And Local Variable With The Same Name
Consider the code given:
val = 1
def func():
val = 50
print("Value of Local variable:", val)
func()
print("Value of Global Variable:", val)
In this, we have declared a global variable val = 1 outside the function func(). Now, we re-declared a local variable inside the function func() with the same name val. Now, we try to print the values of val, inside, and outside the function. We observe the following output:
Value of Local variable:: 50
Value of Global variable:: 1
In the above code, both global and local variables have the same name val. On printing the value of val we get a different result, because the variables have been declared in different scopes, i.e., the local scope inside func() and global scope outside func().
When we print the value of the variable inside func() it outputs Value of Local variable: 50. This is the local scope of the variable. In the local scope, it prints the value that it has been assigned inside the function.
Similarly, when we print the variable outside func(), it outputs Value of Global variable: 1. This is the global scope of the variable, and the value of the global variable val is printed.