Nearby lessons

119 of 159

Python - Global Keyword

📌 What You Will Learn
  • Understand the global keyword
  • Declare a global variable inside a function
  • Modify a global variable from inside a function
  • See what happens when global is not used
  • Access a global variable with globals()

What is the global Keyword?

The global keyword is used inside a function to work with global variables.

By default, when we assign a value to a name inside a function, Python creates a local variable. The global keyword changes this behaviour.

Two Purposes of the global Keyword

We can use the global keyword for the following 2 purposes:

  • To declare a global variable inside a function.
  • To make a global variable available to the function so that we can perform required modifications.

Example 1: Without the global Keyword

Consider what happens when we assign to a name inside a function without using global:

🐍Code Cell
1a = 10
2 
3 
4def f1():
5 a = 777
6 print(a)
7 
8 
9def f2():
10 print(a)
11 
12 
13f1()
14f2()
Output
777
10

Explanation

Inside f1(), a = 777 creates a new local variable named a. It does not modify the global a.

So f1() prints the local value 777, while f2() still sees the original global value 10.

Example 2: Modifying a Global Variable

Now use the global keyword inside f1() so that the assignment changes the actual global variable:

🐍Code Cell
1a = 10
2 
3 
4def f1():
5 global a
6 a = 777
7 print(a)
8 
9 
10def f2():
11 print(a)
12 
13 
14f1()
15f2()
Output
777
777

Explanation

The global a statement inside f1() tells Python that a refers to the global variable, not a local one.

Therefore a = 777 modifies the global variable. Now f1() and f2() both print 777.

Comparison: Without vs With global

Without global With global
Creates a local variable. Refers to the global variable.
Global value is unchanged. Global value is modified.
Other functions still see the old value. Other functions see the new value.

Example 3: Accessing a Local Variable from Another Function

If a variable is declared only inside a function, it cannot be accessed from another function:

🐍Code Cell
1def f1():
2 a = 10
3 print(a)
4 
5 
6def f2():
7 print(a)
8 
9 
10f1()
11f2()
Output
10
NameError: name 'a' is not defined

Explanation

The variable a is local to f1(). When f2() tries to access it, Python cannot find a in any scope and raises a NameError.

Example 4: Declaring a Global Variable Inside a Function

Using global, we can declare a brand new global variable from inside a function:

🐍Code Cell
1def f1():
2 global a
3 a = 10
4 print(a)
5 
6 
7def f2():
8 print(a)
9 
10 
11f1()
12f2()
Output
10
10

Explanation

There is no global a at module level. Inside f1(), the global a statement declares a as a global variable, and a = 10 gives it a value.

Now f2() can access the same global a and prints 10.

When Global and Local Variables Have the Same Name

If a global variable and a local variable have the same name, we can access the global variable inside the function using globals()['name']:

🐍Code Cell
1a = 10 # global variable
2 
3 
4def f1():
5 a = 777 # local variable
6 print(a)
7 print(globals()['a'])
8 
9 
10f1()
Output
777
10

Explanation

Inside f1(), the plain name a refers to the local variable, so print(a) shows 777.

globals()['a'] returns the value of the global variable, which is 10.

Points to Remember

  • global is used only inside a function.
  • It must be written before any use of the variable in that function.
  • Without global, an assignment inside a function always creates a local variable.
  • globals() returns a dictionary of all global names and their values.
  • Use global only when a function really needs to change a module-level variable.
📝 Key Takeaways
  • The global keyword is used inside a function to declare or modify a global variable
  • Without global, assigning a value inside a function creates a local variable
  • With global, the assignment modifies the module-level variable
  • global also lets us declare a brand new global variable from inside a function
  • globals()['name'] reads the global value even when a local variable has the same name

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10