<jupyter_start><jupyter_text>Python follows LEGB Rule:    Local    Enclosing    Global    Built-in<jupyter_code>#Local example 

lambda num:num**2
# Enclosing functions local example
name = 'THIS IS A GLOBAL STRING' # global

def greet():
    name = 'sammy' # enclosed

    def hello():
        name = 'I am a local' # local
        print('Hello ' +name)
    hello()


greet() # when greet func is called - it assigns name to sammy ( internally ) follows LEGB rule
# built in function
len('sammy')
sum([4,3])
x = 50 # scope is Globally

def func():
    global x # this tells python to go in global level and grab global 'x'
    
    print(f'X is {x}')
    
    # local reassignment on a global variable
    x = 'new value' # this x scope is only in this function
    print(f'I just locally change global x to be {x}')
func()
print(x) # global x was changed<jupyter_output>new value
StarcoderdataJupyterStructuredCleanDedup