Python Global Variables Example: Complete Guide with Code Snippets

Logan Jul 13, 2026

Global variables in Python are mutable entities that can be accessed and modified from anywhere within the code, regardless of function or class boundaries. They are defined outside of functions or methods and are available throughout the program, providing a centralized point of access for data that must be shared between multiple parts of an application.

Python Local and Global Variables With Example Programs - EasyCodeBook.com
Python Local and Global Variables With Example Programs - EasyCodeBook.com

Global variables can be quite useful, but they also need to be handled carefully. Misuse can lead to the so-called "globals mafia" issue, where the global scope loses its primacy, and code becomes harder to understand, debug, and maintain. Let's explore some key aspects of global variables in Python, including how to create them, use them, and avoid common pitfalls, with practical examples.

Python Variables Tutorial for Beginners πŸš€
Python Variables Tutorial for Beginners πŸš€

Defining Global Variables

To create a global variable in Python, simply assign a value to a variable outside of any function or class. This variable can then be accessed from anywhere within the code.

Different Types of Variables in Python with Examples
Different Types of Variables in Python with Examples

Here's an example:

total = 0 # This is a global variable

Coding For Beginners Python - Learn the Basics - Variables
Coding For Beginners Python - Learn the Basics - Variables

To demonstrate that we have indeed created a global variable, let's use this variable in a function:

def update_total(num):
    global total
    total += num

In this example, we use the global keyword to indicate that we want to access and modify the global variable total from within the function.

Python variables cheat sheet
Python variables cheat sheet

Accessing Global Variables

To access a global variable, you don't need to do anything specialβ€”just use the variable's name. Here's how you can access the global total variable:

def print_total():
    print(f'The total is: {total}')

What are Python Global Variables
What are Python Global Variables

In this example, the function print_total accesses the global variable total and prints its value.

Global Variable Naming Conventions

Day 2 of becoming a python developer πŸπŸ’» Variables in python
Day 2 of becoming a python developer πŸπŸ’» Variables in python
global variable
global variable
Variables
Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Basics for Beginners – What is Python & Variables
Python Basics for Beginners – What is Python & Variables
Python Developer (@Python_Dv) on X
Python Developer (@Python_Dv) on X
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Global and Local Variables - Interactive Python
Global and Local Variables - Interactive Python
44 Python Local and Global Variable | Online Training Download app from below link
44 Python Local and Global Variable | Online Training Download app from below link
Python Variables
Python Variables
Variables - Naming Rules, Local & Global Variables | Python Tutorials For Beginners
Variables - Naming Rules, Local & Global Variables | Python Tutorials For Beginners
Library vs Module vs Package in Python: Differences and Examples
Library vs Module vs Package in Python: Differences and Examples
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Ionic App
Ionic App
three different types of python programming
three different types of python programming
Python data types cheat sheet
Python data types cheat sheet
a black and white poster with the words python for everything
a black and white poster with the words python for everything
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
Python Variables and Data Types - A complete guide for beginners - DataFlair
Python Variables and Data Types - A complete guide for beginners - DataFlair
Python Variables & Data Types Explained β€” Beginner Guide
Python Variables & Data Types Explained β€” Beginner Guide

Python doesn't have built-in syntactical restrictions for naming global variables, but it follows the PEP 8 style guide like the rest of the language. According to the guide, variable names should be lowercase, with words separated by underscores (snake_case).

Here are some examples of valid global variable names according to PEP 8:

  • my_global_var
  • number_of_students
  • total_amount_owed

Global Variables vs Local Variables

Before we delve deeper into global variables, it's important to understand the difference between them and local variables. Local variables are only accessible within the context of a specific function or block, while global variables are accessible throughout the entire module.

If a variable is defined with the global keyword, even if it's later referenced within a function, it behaves as a global variable. In other words, the global keyword doesn't create a global variable; it makes an existing variable global.

Scoping Issues: Global Variables vs Local Variables

One of the main challenges with global variables is that they can be modified from any part of the code, which can lead to unexpected behavior and bugs that are difficult to track. Consider the following code snippet:

total = 0 # This is a global variable
def update_total(num):
    total += num
    print(f'updated total: {total}')
    return total

print(update_total(10))
print(update_total(20))

You might expect this code to print updated total: 10, then updated total: 20, but that's not what happens. Because total is a global variable, its value is shared between calls to update_total. The output is:

updated total: 10
updated total: 30

Using global Keyword Carefully

While global variables can sometimes simplify code, they can also make it more difficult to understand and harder to debug. It's generally a good idea to minimize your use of global variables and to be very careful when using them. Here are a few best practices:

  1. Minimize the scope of global variables by defining them in functions and then passing them as arguments when necessary. This limits the impact of those variables and makes your code easier to reason about.
  2. Avoid modifying global variables within functions unless that's the whole point of the function. In other words, don't use the global keyword lightly.
  3. When using global variables, use snake_case variable names.
  4. Consider using classes or modules instead of global variables to organize your code, especially if the global data tends to cluster around certain functions or methods.

Finally, remember that the power of global variables is also their danger. They can make your code more flexible, but they can also make it harder to understand and debug. Use them judiciously, and only when they're clearly the right tool for the job.