6.2.5 The global Keyword & LEGB Scope Rule

Unit: Unit 6: Functions • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Modify global variables using the 'global' keyword
total_visitors = 0

def record_visit():
    global total_visitors  # Declare intent to modify the global variable
    total_visitors = total_visitors + 1

record_visit()
record_visit()
record_visit()

print("Total visitors recorded:", total_visitors)
Open in Live Interactive Python Sandbox →