Global Variable in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What is a Global Variable in Python?

A global variable in Python is a variable declared outside of the function or in the global scope. A global variable can be accessed from both inside and outside the function. A variable that you want to use throughout the scope of a program should be declared globally.

Create a Global Variable in Python

To make a global variable in Python, you must declare it outside of the function or in the global scope.

Output:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How to Access the Global Variable Inside and Outside of a Function?

Let's see how we can access a global variable when it is used inside and outside a function:

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Using Global Variables in Python Functions

The global Keyword

The global keyword is used to declare that a variable inside the function is referencing a global variable.

This allows you to modify the global variable from within the function's scope.

In this example, the check_global() function modifies the value of the global variable num_1 by using the global keyword.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

The globals() Function

The globals() function in Python returns a dictionary representing the global symbol table. This dictionary includes all the global variables in the current global scope, including built-in variables.

Here's how you can use the globals() function:

When you call my_function(), it prints out a dictionary containing all the global variables available at that point in the program.

Local Variables

  • Variables that are declared within a function's scope are called as local variables.
  • These variables are only accessible within the function where they are defined.

In this example, number is a local variable defined within the local_function() function. Attempting to access number outside of the function will result in a NameError because it's not defined in the global scope:

Difference Between Global and Local Variables

Here's a table summarizing the differences between local and global variables in Python:

AspectLocal VariablesGlobal Variables
DefinitionDefined inside a function or block.Defined outside of all functions.
ScopeAccessible only within the function/block where they are declared.Accessible throughout the entire program, including inside functions.
LifetimeExist only during the execution of the function/block. They are destroyed once the function/block execution is completed.Persist throughout the life of the program.
DeclarationDeclared within the function/block. No special keyword required.Declared outside any function. If modification is needed inside a function, the global keyword must be used.

This table highlights the fundamental distinctions between local and global variables in terms of their scope, lifetime, declaration practices, and their use within Python programming.

Now, we'll see how global and local variables behave in the same code, through an example:

Output:

In the above code, we declare the variable x as global and y as local inside the function. Next, we perform an operation on the variable x, and leave y as it is. Then we call the function and observe that the value of x comes out to be 10, as we multiplied x by 5. We also print the value of y.

Conclusion

  • Global variables are those variables that can be accessed both inside and outside functions in a program.
  • Global variables should be declared outside a function.
  • Local variables are declared inside a function, and their scope is limited to that function only. This is what differentiates them from global variables.
  • To declare a global variable inside a function, the word global should be used to initialize the variable. Example: global x, where x is the name of the variable.

Learn More

Variables in Python

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more