Global and Local Scope
Global and Local Scope
April 23
• 1. If return statement is not used inside the function, the function will
return:
• 2.Find the errors in following function definitions :
• More than one namespace can exist at an instant, but they all work
isolated.
• Due to this isolation, two namespaces can have the same name and
these will be independent of each other.
• It can be thought of as a dictionary, where name-object pairs exist.
• The lifecycle of a namespace is the time for which the object in the
scope is accessible. When the scope of the object ends, the object
gets deleted. Thus different objects have different time duration for
which it exists in the memory, which decides the life cycle.
• The life cycle reduces from built-in namespace to global namespace
to inner namespaces.
Learning Objectives
• Understand what is a global variable in Python with examples
• Use global variables across multiple functions
• Learn how to use the global keyword to modify the global variables
• Learn to use global variables across Python modules/files
• Use global variables inside a nested function
we use variables in every programming language .
Variables store the value (variables refer to containers which store data values) and are usually known as
the location or space in the main memory.
Sometimes we use variables within the function, or sometimes, we use outside the function. The scope
of variables in python completely depends on where we declare the variables.
Global Scope
we use variables in every programming language .
Variables store the value (variables refer to containers which store data values) and are usually known as the
location or space in the main memory.
Sometimes we use variables within the function, or sometimes, we use outside the function. The scope of
variables in python completely depends on where we declare the variables.
Scope of Variables
Part(s) of program with in which a name or variable is legal and accessible. The location where one can find a
variable and also access
if we declare the variable outside the function, we can access it anywhere in the program. That means we
can access it in the function also. This concept is known as the global scope in python.
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.
global scope and global keyword in python are two different things. In the global scope, we declare variables
outside the function. While using the global keyword in python we declare the variable inside the function
preceded with the global keyword.
There are two kinds of Scope