Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Modifying a Global Variable in Python | Understanding Variable Scope in Python
Intermediate Python Techniques

book
Challenge: Modifying a Global Variable in Python

Task

Swipe to start coding

This task shows how you can modify a global variable from within a function using the global keyword.

  1. Use the keyword global for receiving access to modify global_var;
  2. Increase global_var value on to 5;
  3. Call the function modify_global to implement changes;
  4. Print modified global_var.

Solution

global_var = 10

def modify_global():
global global_var
global_var += 5

modify_global()
print("Modified global variable:", global_var)

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
single

single

global_var = 10

def modify_global():
___ global_var
___ += 5

___()
print("Modified global variable:", ___)

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt