Python Tutorials · Python Variables

Global Variables

Learn all about Global Variables in this comprehensive tutorial.

5 min read beginner
  • Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.
  • Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

Global Variables

Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

python

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

python

The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

python

Also, use the global keyword if you want to change a global variable inside a function.

python

Module quiz

2 questions
1

Which of the following is true about Global Variables?

2

What is the most common pitfall when working with Global Variables?

Answer all questions to submit.