Python utilizes a scope resolution rule known as LEGB, which dictates the order in which Python searches for variable names:
Local (L):
This scope encompasses names defined within the current function. If a variable name is found in the local scope, Python uses that value.
Enclosing function locals (E):
If the name is not found locally, Python searches the scopes of any enclosing functions (in nested functions).
Global (G):
If the name is not found in any enclosing scopes, Python looks in the global scope, which includes names defined at the top level of the module.
Built-in (B):
Finally, if the name is not found in the global scope, Python checks the built-in scope, which contains pre-defined functions and constants.
If a variable is assigned a value within a function, it is generally considered local to that function, unless explicitly declared as global using the global keyword. Variables defined outside of any function are considered global and can be accessed from within functions, although their values can only be modified within a function if declared global.