0% found this document useful (0 votes)
2 views2 pages

Python Local Variables

Local variables in Python are declared within a function and cannot be accessed outside of it, leading to a NameError if referenced externally. In contrast, global variables are declared outside of functions and can be accessed both inside and outside of functions. This distinction is crucial for understanding variable scope in Python programming.

Uploaded by

Varshitha Kn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Local Variables

Local variables in Python are declared within a function and cannot be accessed outside of it, leading to a NameError if referenced externally. In contrast, global variables are declared outside of functions and can be accessed both inside and outside of functions. This distinction is crucial for understanding variable scope in Python programming.

Uploaded by

Varshitha Kn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Local Variables

When we declare variables inside a function, these variables will have a local scope (within the
function). We cannot access them outside the function.

These types of variables are called local variables. For example,

Output

Local Hello

NameError: name 'message' is not defined

Python Global Variables

In Python, a variable declared outside of the function or in global scope is known as a global
variable. This means that a global variable can be accessed inside or outside of the function.

Let's see an example of how a global variable is created in Python.

You might also like