Python Global, Local and Nonlocal variables
In this article, you will learn variable Python as variable nonlocal variable and where used.
Table of Contents
- Global Variables in Python
- Local Variables in Python
- Global and Local Variables Together
- Nonlocal Variables in Python
Global Variable in Python:
Python, a declaration of change outside of the work or scope of international variable variables known worldwide. This means international variables are available inside or outside of the activity.
Let's see examples of how an international change has been created in Python.
Example 1: Create a Global Variable
x = "global"
def foo():
print("x inside :", x)
foo()
print("x outside:", x)
When we codify the code, an output will be:
x inside : global
x outside: global
In the preceding lines, we create a variable x which defines foo () that will print x variable. Finally, we call an f () value x will be published.
What if you want to change the value of x inside?
x = "global"
def foo():
x = x * 2
print(x)
foo()
When we codify the code, an output will be:
UnboundLocalError: local variable 'x' referenced before assignment
The output shows errors because it is about Python x as a variable and x is not defined in the foo ().
Use this task by using the global keyword, to find out more and visit the keyword in Python.
Local Variables in Python:
A description of the physical function or the size of the local changes is known locally.
Example 2: Accessing local variable outside the scope
def foo():
y = "local"
foo()
print(y)
When we codify the code, an output will be:
NameError: name 'y' is not defined
The output shows the error because we are trying to locate internal changes and international and local changes just like the foo () or local boundary.
Let's see examples of how Python created local changes.
Example 3: Create a Local Variable
Usually, we can declare functional variables to create a local change.
def foo():
y = "local"
print(y)
foo()
When we vote in the code, it will be:
local
Let's look at previous problems, x is an international animal and we want to change x ().
Global and Local Variables Together:
Next, it will show you how to use international variables and internal code changes are the same.
Example 4: Using Global and Local variables in the same code
x = "global"
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
When we codify the code, an output will be:
global global
local
In the preceding code, we declare x and y as an international variable such as the local variable (). Next, we use multilanguage staff * alter global variable x and print x and y both.
After you call the foo (), the value x is the world because x * 2 is used to print twice in the world. After that, we publish the local value-added change.
Example 5: Global variable and Local variable with the same name
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
When we codify the code, an output will be:
local x: 10
global x: 5
In the previous article, the same name has been used both in terms of internal changes and international trends. I get different results when the same changes are printed because the variables are specified on both sides, that is, inside () and outside the scope of the world (foo).
When printing non-inside (), which comes out of x: x 10, it is called the size of the local stock.
Also, when we print the () variable (x), the global x-ray of x: 5, known as the 'global' scale.
Nonlocal Variables in Python
Non-residential changes used in intermediate activities are not defined in the environment. This means that variables can be maintained locally or internationally.
Let's see examples of how an international change has been created in Python.
We use keywords to make non-volatile variables.
Example 6: Create a nonlocal variable
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
When we codify the code, an output will be:
inner: nonlocal
outer: nonlocal
The first item is the internal action (). We use keywords to make non-volatile variables. An intelligent idea () is defined as the number of other functions outside ().
Note: If you change the value of the inputs, the changes appear in the internal variables.
Comments
Post a Comment