0% found this document useful (0 votes)
14 views

Global and Local Scope

Python namespaces are collections of name-object mappings that provide isolation so multiple namespaces can have the same name without interfering with each other; global namespaces have the longest lifespan while built-in namespaces are always available, and local namespaces exist only within their defined scope like a function. Python uses a concept of scopes to determine where variables are accessible rather than block structure used in other languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Global and Local Scope

Python namespaces are collections of name-object mappings that provide isolation so multiple namespaces can have the same name without interfering with each other; global namespaces have the longest lifespan while built-in namespaces are always available, and local namespaces exist only within their defined scope like a function. Python uses a concept of scopes to determine where variables are accessible rather than block structure used in other languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Precap – 4th 

April 23
• 1. If return statement is not used inside the function, the function will
return:
• 2.Find the errors in following function definitions :

• 3.What are formal arguments ? What are actual parameters ? How


are these two terms different yet related ? Give example.
A Python
name is an
identifier
•What is Python Namespaces?

•A namespace in python is a collection of names. So, a namespace is


essentially a mapping of names to corresponding objects.

• 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

Answer 1: The built-in scope has all


the names which are loaded into the
python variable scope when one
Question 1: What is a built-in scope
starts the interpreter. For instance, Question 2: Does Python have scope?
in Python?
one does not need to import any
module for accessing functions like
print () and id ().

Answer 2: In Python language,


Answer 3: There are two kinds of
dissimilar to other languages like C,
variables. They are global variables
Python variable is in scope for the
and local variables.
whole of the function or class or Question 3: What is the local and
The scope of global variables refer to
module where it is displayed and not global scope in Python?
the entire program while
merely in the innermost ‘block’.
the scope of local variable limits to
Moreover, we don’t need to declare
the function where it is defined.
variables in Python.

You might also like