global
KeywordPython 2.0+Intermediate
Declares a variable inside a function as belonging to the global scope
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
count = 0 def increment(): global count count += 1 increment() increment() increment() print(f"Count: {count}") # Without global, assignment creates a local variable x = "global" def no_global(): x = "local" # creates a new local x print(f"Inside: {x}") no_global() print(f"Outside: {x}")
Without 'global', assigning to a variable inside a function creates a local variable. 'global' tells Python to use the module-level variable instead.
Try in PlaygroundTags
languagesyntaxcorescopevariable