Python Central HubSign in

Python Programming / Python Variables

Global Variables

An introduction to global variables in Python. Learn about global variables, local variables, and the global keyword. This tutorial is part of the Python Variables Course.

We can create a variable outside of a function. This is called a global variable. We can access the global variable inside or outside of a function. Let's see an example.

A variable's scope is the region of the program where it is visible. A variable created outside every function lives in the global scope; one created inside a function lives in that function's local scope.

Diagram:

diagram global vs local scope mermaid
Where variables are visible

Example:

Python
# Global variable
name = "John"
def display():
    print("Hello, " + name)
display()
print("Hello, " + name)

Output:

cmd
C:\Users\Your Name> python variable.py
Hello, John
Hello, John

In the above example, we created a global variable name outside of the function display(). We can access the global variable name inside or outside of the function display().

Global Keyword

We can create a local variable inside a function. This is called a local variable. We can access the local variable inside the function. We can't access the local variable outside the function. Let's see an example.

Example:

Python
# Global variable
name = "John"
def display():
    # Local variable
    name = "Smith"
    print("Hello, " + name)
display()
print("Hello, " + name)

Output:

cmd
C:\Users\Your Name> python variable.py
Hello, Smith
Hello, John

In this example, we created a local variable name inside the function display(). We can access the local variable name inside the function display(). We can't access the local variable name outside the function display().

We can use the global keyword to access the global variable inside the function. Let's see an example.

Example:

Python
# Global variable
name = "John"
def display():
    # Local variable
    global name
    name = "Smith"
    print("Hello, " + name)
display() 
print("Hello, " + name) 

Output:

cmd
C:\Users\Your Name> python variable.py
Hello, Smith
Hello, Smith

In this example, we used the global keyword to access the global variable name inside the function display(). We can access the global variable name inside the function display(). We can also access the global variable name outside the function display().


Try it: Global Variables Exercises

Exercise 1 – Access Global Variable

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Modify Global with global Keyword

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Global vs Local

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did