HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Python Scope


A variable is only available from inside the region it is created. This is called scope.


Types of Scope

Local Scope

A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.

Example
 def myfunc():
    x = 300
    print( x)

myfunc() 

Output
   300           

Global Scope

A variable created in the main body of the Python code is a global variable and belongs to the global scope.Global variables are available from within any scope, global and local.

Example
  x = 300

   def myfunc():
   print(x)

   myfunc()

   print( x)  

Output
   300 
   300