Python Local, Global Variables and Globals Function for Beginners with Examples
In this post, we’ll learn Python Local and Global Variables with Examples for Beginners. Every programming language has its own way of declaring local and global variables. Usually, local variables have local scope i.e they are declared inside a function and global variables are declared outside function or class. Compared to local variables global variables have a larger scope and can be called in any part of the program.
Table of Contents
Introduction
Python Local and Global Variables are used to store value at local and as well as global levels based on program requirements. Many times we require variables that we can use not only inside the function but also outside.
Declaring Local Variables
Variables declared as local can only be used inside a block of code.
def my_func(): age = 25 print("Age is : {}".format(age)) my_func() #PYTHON OUTPUT Age is : 25
def my_func(): age = 25 my_func() print("Age is : {}".format(age)) #PYTHON OUTPUT NameError: name 'age' is not defined
Python raises the exception NameError when we try to access the local variable age outside the function.
Declaring Global Variables
Global variables can be used inside any function or can be used in other modules.
name = "Clark" def my_name(): print("My Name is : {}".format(name)) my_name() #PYTHON OUTPUT My Name is : Clark
name = "Clark" def my_name(): name = "Bruce" print("My Name inside function is : {}".format(name)) my_name() print("My Name outside function is : {}".format(name)) #PYTHON OUTPUT My Name inside function is : Bruce My Name outside function is : Clark
If we try to modify a global variable from the inside function then that will create a local variable. To only change the value of the global variable use the global keyword.
name = "Clark" def my_name(): global name name = "Bruce" print("My Name inside function is : {}".format(name)) my_name() print("My Name outside function is : {}".format(name)) #PYTHON OUTPUT My Name inside function is : Bruce My Name outside function is : Bruce
globals() Function
The globals() function is a built-in function in python that contains a dictionary of global variables. Just like a dictionary we can access these variables and update their values.
name = "Suresh" marks = { "English" : 68, "Maths" : 56, "Science" : 87, } def change_marks(): print("Marks Scored By {}\n".format(name)) for index, subject_marks in globals()['marks'].items(): subject_marks += 2 print("{} : {}\n".format(index, subject_marks)) # Dictionary of Global variables print("Global Variables : {}\n\n".format(globals())) change_marks() # Updating value of Global variable globals()["marks"]["English"] = 44 #Updated value of english print("\n\nUpdated Global variable marks english : {}\n\n".format(globals()["marks"]['English'])) #PYTHON OUTPUT Global Variables : {'__cached__': None, '__package__': None, '__file__': 'vars.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f23229d9940>, '__name__': '__main__', '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, 'marks': {'English': 68, 'Maths': 56, 'Science': 87}, '__spec__': None, 'change_marks': , 'name': 'Suresh'} Marks Scored By Suresh English : 70 Maths : 58 Science : 89 Updated Global variable marks english : 44
Conclusion
You can reach the end of our post on Python Local and Global Variables with Examples for Beginners. we appreciate you for going through this post. If you find anything difficult with our way of explanation please leave us a comment.
Related Posts
- What is Python all() function and how it works for beginners?
- What makes the Python Property() function and Decorator so amazing?




