<jupyter_start><jupyter_text>LEGB format of rules L - Local : assigned within a function (def and lambda, and not declared globally even for that functionE - Enclosing Function Locals : Names in local scope of any and all enclosing functions from inner to outerG - Global : Assigned at top level of file, defined throughout the fileB - Built-in : Pre-assigned in built-in name modules..open,range,SyntaxError<jupyter_code>lambda num:num**2 #num is local
name = 'This is a global String'

def greet():
    #Enclosing
    name = 'Yeet'
    
    def hello():
        #Local
        name = "I'm a local"
        print('Hello '+name)
        
    hello()
greet()
len
help(len)
x = 50

def func(x):
    print(f'x is {x}')
    
    #Local
    x = 200
    print(f'x has been changed to {x} locally')
func(x)<jupyter_output>x is 50
x has been changed to 200 locally<jupyter_text>Local Scopes don't affect higher level scopes<jupyter_code>x = 50

def func(t): #now this function doesn't recieve any x variable, cause x is already defined to be the global one
    global x #this makes all x in this function as the global variable declared earlier.
    print(f'x is {x}')
    
    #Local
    x = 'New Value'
    print(f'x has been changed to {x} locally')
func(False)
func('char')
func(10)
x<jupyter_output><empty_output><jupyter_text>but global reassignment by local functions by global keyword should be avoided, as it created confusion. For reassignment, better to return the value, and reassign it later.This is safer...easier to debug, as more clear.<jupyter_code>from collections import Counter
myList = ['a','a','b','c','c','c','a']
print(Counter({'a':1, 'b':2, 'c':3})) #i don't understand how this worked..<jupyter_output>Counter({'c': 3, 'b': 2, 'a': 1})
{'fraction_non_alphanumeric': 0.11180832857957787, 'fraction_numerical': 0.011979463776383342, 'mean_word_length': 3.885793871866295, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '283594', 'license': 'Apache-2.0', 'n_tokens_mistral': 588, 'n_tokens_neox': 555, 'n_words': 222, 'path': 'Nested_Statements_and_Scope.ipynb', 'repo_name': 'ba-13/PythonBasics', 'size': '1753'}