Python global Keyword

❮ Python Keywords


Example

Declare a global variable inside a function, and use it outside the function:

#create a function:
def myfunction():
  global x
  x = "hello"

#execute the function:
myfunction()

#x should now be global, and accessible in the global scope.
print(x)
Try it Yourself »

Definition and Usage

The global keyword is used to create global variables from a no-global scope, e.g. inside a function.


❮ Python Keywords

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.