Scope refers to the region of a program where a variable is accessible.

A variable cannot be used everywhere — it is available only within a certain block or function, depending on where it is defined.


Local Scope :

               A variable declared inside a function belongs to the local scope. It is accessible only inside that function.

               Trying to access it outside function raises error

Python
def greet(): message = "Hello" print(message) greet() #output Hello 


Global Scope :

               A variable declared outside all functions is a global variable.

It is accessible:

 inside functions
 outside functions
 throughout the program

Python
language = "Python" def show(): print(language) show() print(language) #output Python Python 


Global Keyword :

               By default, if you modify a global variable inside a function - Python treats it as local and throws an error.

               To modify it — use the global keyword.

Python
count = 0 def update(): global count count += 1 update() print(count) #output 1


LEGB Rule :

Scope TypeWhere DefinedAccessible In
Localinside functionthat function only
Enclosedouter functioninner function
Globaloutside functionsentire program
Built-inPython systemeverywhere