Functions II

Accessing Global Variables in a Function

In this lesson, you'll learn about about the concept of global and local scopes, how to access and modify global variables inside a function, and how to use the global keyword in Python.


We have already learnt that variables store values for later use. To access the stored value, we just have to call a variable somewhere below in our code. For example:

text = 'Hello World' print(text)
Python

We can even access that variable from within a function. Let's try it:

text = 'Hello World' def my_function(): print(text) my_function()
Python
Output

However, the rules change slightly when we want to modify the variable inside the function.

Let's see what happens if we try to:

text = 'Hello World' def my_function(): text = 'Python' my_function() print(text)
Python
Output

Even after calling the function, the variable text still holds its original value.

It seems like nothing changed, even though we assigned a new value to text inside the function.

To understand what happened here, you need to know that we are dealing with two so-called scopes.

A scope describes a region of code from which you can access certain variables or functions.

When we first created the variable text, we were creating it in the global scope.

##### global scope ##### text = 'Hello World' # global variable print(text)
Python

The global scope is the outermost scope in your program. Whenever you create a variable outside a function (or a class), you are defining a global variable within the global scope.

However, when you create a function, the code inside the function has its own scope, known as local scope.

#### global scope #### ... def my_function(): #### local scope #### ...
Python

You can access all global variables inside the local scope of a function. For example:

#### global scope #### my_num = 10 def my_function(): #### local scope #### print(my_num) # local variable my_function()
Python
Output

However, when we create a variable inside the function, we're defining a so-called local variable that can only be accessed in the local scope of that function.

For example, here we create a local variable my_num inside the local scope of my_function. When we try to access that variable outside the function, we run into an error:

def my_function(): #### local scope #### my_num = 10 # local variable #### global scope #### my_function() print(my_num)
Python
Output

Back to our original example and to why the value of text does not change:

text = 'Hello World' def my_function(): text = 'Python' my_function() print(text)
Python
Output

The reason is that we have actually created 2 versions of text: one in the global scope and another that exists only within the scope of my_function.

Here, you can see that the string Python is only assigned to the local version of text, while the global version remains unchanged before and after we call my_function:

#### global scope #### text = 'Hello World' print(f"Global scope: '{text}'") def my_function(): #### local scope #### text = 'Python' print(f"Local scope: '{text}'") #### global scope #### my_function() print(f"Global scope: '{text}'")
Python
Output

To tell Python that you want to modify a global variable inside a function instead of creating a new one with the same name, you can use the global keyword.

You can use the global keyword to declare a variable as global within a local scope:

def my_function(): #### local scope #### # declare my_var as global global my_var
Python

This allows my_var to be accessed and modified globally.

So, to modify the global variable text from our example, we need to declare it as global inside my_function:

#### global scope #### text = 'Hello World' print(f"Global scope: '{text}'") def my_function(): #### local scope #### # declare text as global: global text text = 'Python' print(f"Local scope: '{text}'") #### global scope #### my_function() print(f"Global scope: '{text}'")
Python
Output

Now, we have successfully modified text from within the scope of my_function.

Using the global keyword, you can not only modify global variables from within local scopes, but also create new ones.

For example, in the code below, we create a new global variable inside my_function and access it outside the function's scope:

def my_function(): global num num = 10 my_function() print(num)
Python
Output

Be aware that the variable is only created when you call the function.

It will not work if you try to access num before calling my_function:

def my_function(): global num num = 10 print(num) my_function()
Python
Output

What will be the output?

price = 10 def func(): quantity = 2 print(price * quantity) func()
Python

What will be the output?

price = 10 def func(): quantity = 2 func() print(price * quantity)
Python

What will be the output?

price = 10 def func(): price = 5 print(price) func() print(price)
Python

What will be the output?

price = 10 def func(): price = 5 print(price) print(price) func()
Python

What will be the output?

price = 10 def func(): price = 5 print(price) func() print(price)
Python

What will be the output?

price = 10 def func(): global price price = 5 print(price) func() print(price)
Python