In Python, variables refer to containers which store data values. Basically, they are reference or pointers to an object in memory. In other words, it means that whenever a variable is assigned to an instance, it will be mapped to that instance. Moreover, contrasting to languages like C/C++/JAVA, Python is not statically typed. Meaning to say, one does not need to declare variables before we use them or declare their type. Consequently, a variable forms when we first assign a value to it. Thus, this article will throw light on the scope of variables.
# Python program to demonstrate # variable assignment # An integer assignment age = 30 # A floating point salary = 2000.5 # A string name = "Ken" print(age) print(salary) print(name)
30 2000.5 Ken
Scope of Variable
The location where one can find a variable and also access it if the need is referred to as the scope of variables. Global variables refer to the ones which are defined and declared outside any function are do not specify to any function. They come in use in any part of the program.
# This function uses global variable s def f(): print(s) # Global scope s = "I love Toppr" f()
I love Toppr
Now, suppose that a variable having the same name is defined inside the scope of function as well. Then, it will print the value given inside the function only and not the global value.
# This function has a variable with # name same as s. def f(): s = "Same here." print(s) # Global scope s = "I love Toppr" f() print(s)
Same here. I love Toppr
Browse more Topics Under Defining Functions
- Invoking Functions
- Passing Parameters
- Void Functions and Functions Returning Values
- Flow of Execution
In Python, we make use of nonlocal keywords in the case of nested functions. The keyword operates similar to the global. But, instead of global, this keyword declares a variable to point to the variable of outside enclosing function, in case of nested functions.
# Python program to demonstrate # nonlocal keyword print ("Value of a using nonlocal is : ", end ="") def outer(): a = 5 def inner(): nonlocal a a = 10 inner() print (a) outer() # demonstrating without non-local # inner loop not changing the value of outer a # prints 5 print ("Value of a without using nonlocal is : ", end ="") def outer(): a = 5 def inner(): a = 10 inner() print (a) outer()
Value of a using nonlocal is: 10 Value of a without using nonlocal is: 5
FAQ on Scope of Variables
Question 1: What is a built-in scope in Python?
Answer 1: The built-in scope has all the names which are loaded into the python variable scope when one starts the interpreter. For instance, one does not need to import any module for accessing functions like print () and id ().
Question 2: Does Python have scope?
Answer 2: In Python language, dissimilar to other languages like C, Python variable is in scope for the whole of the function or class or module where it is displayed and not merely in the innermost ‘block’. Moreover, we don’t need to declare variables in Python.
Question 3: What is the local and global scope in Python?
Answer 3: There are two kinds of variables. They are global variables and local variables. The scope of global variables refer to the entire program while the scope of local variable limits to the function where it is defined.