In Python, variables are the containers for storing data values. They are reference, or pointers, to an object in memory which means that whenever a variable is assigned to an instance, it gets mapped to that instance. Unlike other languages like
Python is not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.
45 1456.8 John
Note: To know more about variables click here.
Scope of variable
The location where we can find a variable and also access it if required is called the scope of a variable.
Global and local variables
Global variables are the ones that are defined and declared outside any function and are not specified to any function. They can be used by any part of the program.
I love Geeksforgeeks
Now suppose a variable with 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.
Me too. I love Geeksforgeeks
The variable s is defined as the string “I love Geeksforgeeks”, before we call the function
f(). The only statement in
f() is the
print(s) statement. As there is no local s, the value from the global s will be used.
The question is, what will happen if we change the value of
s inside of the function
f()? Will it affect the
global s as well? We test it in the following piece of code:
Traceback (most recent call last): File "/home/370cac45bae7f1e6096520b7a0edb604.py", line 13, in
f() File "/home/370cac45bae7f1e6096520b7a0edb604.py", line 3, in f print(s) UnboundLocalError: local variable 's' referenced before assignment
To make the above program work, we need to use
global keyword. We only need to use
global keyword in a function if we want to do assignments / change them.
global is not needed for printing and accessing. Why? Python “assumes” that we want a local variable due to the assignment to s inside of
f(), so the first print statement throws this error message. Any variable which is changed or created inside of a function is local, if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword
global, as can be seen in the following example:
Python is great! Look for Geeksforgeeks Python Section Look for Geeksforgeeks Python Section
Consider the below example for better understanding of the topic.
global : 1 Inside f() : 1 global : 1 Inside g() : 2 global : 1 Inside h() : 3 global : 3
nonlocal keyword is used in the case of nested functions. This keyword works similar to the global, but rather than global, this keyword declares a variable to point to the variable of outside enclosing function, in case of nested functions.
Value of a using nonlocal is : 10 Value of a without using nonlocal is : 5
- Namespaces and Scope in Python
- Scope Resolution in Python | LEGB Rule
- Python | Accessing variable value from code scope
- Python | Scope resolution when a function is called or defined
- Python Variables
- Private Variables in Python
- Python | Unpack whole list into variables
- Python | Extract key-value of dictionary in variables
- Class or Static Variables in Python
- Tracing Tkinter variables in Python
- Global and Local Variables in Python
- Python Program to Swap Two Variables
- How are variables stored in Python - Stack or Heap?
- Inserting variables to database table using Python
- How to assign values to variables in Python and other languages
- Python - Construct variables of list elements
- Swap two variables in one line in C/C++, Python, PHP and Java
- Python - Pearson Correlation Test Between Two Variables
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python | Assign multiple variables with list values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.