Defining Functions

Scope of Variables

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.

scope of variables

Example

                    
# 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)

Output:

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.

Example

                    
# This function uses global variable s

def f():

print(s)

# Global scope

s = "I love Toppr"

f()

Output:

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)

Output:

Browse more Topics Under Defining Functions

Nonlocal Keyword

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.

Example

                    
# 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()

Output

                    
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.

Share with friends
Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started
Customize your course in 30 seconds

Which class are you in?

No thanks.