Local and global scope

def my_function():
    x = 10  # Local variable
    print(x)

my_function()  # Output: 10
print(x)  # Error: NameError: name 'x' is not defined
10
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 6
      3     print(x)
      5 my_function()  # Output: 10
----> 6 print(x)  # Error: NameError: name 'x' is not defined

NameError: name 'x' is not defined
x = 10  # Global variable

def my_function():
    print(x)

my_function()  # Output: 10
print(x)  # Output: 10
10
10
x = 10  # Global variable

def my_function():
    global x
    x = 20  # Modifying the global variable
    print(x)

my_function()  # Output: 20
print(x)  # Output: 20
20
20