In this post, we’ll learn what Python global variable is and how to properly define and use it in Python code.
To better understand the usage of Python global variable, we’ll be using multiple practical examples with proper step by step explanation.
After reading this post, I’m sure you’ll be able to easily define and use Python global variables in your own code.
So what are we waiting for? Let’s just dive right into its practical implementation.
- What is Python Global Variable?
- Implementing Python Global Variable (Multiple Examples)
- Example 1: Creating a Global Variable
- Example 2: Modify Global Variable From inside a Function (Global Keyword)
What is Python Global Variable?
- In Python, a variable is used to store some data.
- If the variable is declared outside of Python function or at a global scope, then this variable is called global variable.
- This type of global variable can be accessed from outside or even inside of Python functions.
Let’s now understand the practical implementation of Python global variable.
Implementing Python Global Variable (Multiple Examples)
The following code examples will demonstrate how Python global variable can be defined and used practically.
Example 1: Creating a Global Variable
value='Global variable' // global variable created def fun(): print(value+' used inside function') fun() // call this function print(value+' used outside function')
Global variable used inside function Global variable used outside function
As you can see, we’ve first created a global variable. Then we’ve fetched its value from both outside and inside of a function successfully.
Example 2: Modify Global Variable from inside a Function (Global Keyword)
Let’s now customize the value of Python global variable from inside a function. See below code:
value='Global variable' def fun(): value='Local variable' // new local variable created print(value) // local variable fun() print(value) // Global variable
You can see that we’ve used the same name of the global variable inside the function but still when we try to print the global variable’s value outside function then it shows the old value.
In Python, creating a variable inside a function as implemented in the above code will create a new variable. Its local so there is no conflict between the names of those local and global variables.
If you want to change the value of global variable inside a function then you’ve to use the global keyword. See below code:
value='Global variable' def fun(): global value // specify that its a global variable using the global keyword value='Modified Global variable' print(value) // Modified Global variable fun() //call the function print(value) // Modified Global variable
Modified Global variable Modified Global variable
As we can see here that the value of global variable is now customized. We’ve changed the value of global variable from inside a function using the global keyword.
For demonstration, we have fetched the value of global variable from both outside and inside the function and it shows the newly modified value.
So this is how you can easily define and use Python global variable in your own code as well.
Feel free to ask if you still have any questions related to the implementation of Python global variable. I’ll be very happy to answer all your questions.
To conclude this post, now you have a complete practical understanding of how to properly use Python global variable. I’ll be looking forward to have your valuable feedback. Thank you for reading this post.