Global variables are the one that are defined and declared outside a function and we need to use them inside a function.
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.
Me too. I love Geeksforgeeks.
If 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.
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:
Line 2: undefined: Error: local variable 's' referenced before assignment
To make the above program work, we need to use “global” keyword. We only need to use the 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:
Now there is no ambiguity.
Python is great! Look for Geeksforgeeks Python Section. Look for Geeksforgeeks Python Section.
A good Example
global : 1 Inside f() : 1 global : 1 Inside g() : 2 global : 1 Inside h() : 3 global : 3
This article is contributed by Shwetanshu Rohatgi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python program to find number of local variables in a function
- Find indices of all local maxima and local minima in an Array
- Global keyword in Python
- Use of nonlocal vs use of global keyword in Python
- What is the Python Global Interpreter Lock (GIL)
- Create Local Binary Pattern of an image using OpenCV-Python
- Python Variables
- Python Scope of Variables
- Private Variables in Python
- Python Program to Swap Two Variables
- Python | Unpack whole list into variables
- Tracing Tkinter variables in Python
- Class or Static Variables in Python
- Python | Extract key-value of dictionary in variables
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python - Construct variables of list elements
- Inserting variables to database table using Python
- Python - Pearson Correlation Test Between Two Variables
- How to assign values to variables in Python and other languages
- Swap two variables in one line in C/C++, Python, PHP and Java