x=4
def func():
print("HELLO WORLD")
y=x+2
x=2
print (y)
print (x) # OUTPUT IS 6,2,2
global x # global declaration is done here
func()
print (x) # outputs as 2 but why???? why not 4????
Why it shows the output as 6,2,2. Indeed i made the print (x) before the global declaration.But i didnt change x value after global declaration but why its printing the x value as 2 after the func().Is it not a sequential execution of statements? or does it read the entire code in the function and then start executing the function line on line? please clear the above program.Thank you in advance
The
globalkeyword indicates to the compiler that the variable is to be considered global throughout the function. It doesn’t matter where it shows up in the function, so long as the compiler takes note of it.