PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Global Variables

Python Global Variables

Updated on: October 21, 2022 | 1 Comment

In this tutorial, you’ll learn what is a global variable in Python and how to use them effectively.

Goals of this lesson:

  • Understand what is a global variable in Python with examples
  • Use global variables across multiple functions
  • Learn how to use the global keyword to modify the global variables
  • Learn to use global variables across Python modules/files
  • Understand the use of globals() function
  • Use global variables inside a nested function

Table of contents

  • What is a Global Variable in Python
  • Using Global Variables In Function
    • Global Variable and Local Variable with Same Name
  • global Keyword in Python
    • Modify a global variable inside a function
    • Create a global variable inside a function
    • Rules of global keyword
  • Global Variables Across Python Modules/Files
  • globals() function in Python
  • Global variables in Nested Function

What is a Global Variable in Python

In Python, a variable declared outside the function or in global scope is known as a global variable. We can use global variables both inside and outside the function.

The scope of a global variable is broad. It is accessible in all functions of the same module.