14 Variable Scope

Scope of variable: It is the range or perticular part of code in which the variable is accessible. Basically there are two types of variables in python depending on the scope of the variable.

  1. Global variables

  2. Local variables

Lets know them one by one.

1) Global Variable:

Variables which can be accessible from all part of the code and defined globally are global variables.

  • Global variables generally not defined inside the function, so their scope is global.

  • They are not limited to the function.

  • They can be accessible from inside as well as outside of the functions.

Lets define global varible my_num, which is easily accessible outside as well as inside of function make_twice().

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Lets define global variable my_num
my_num=int(input("please enter number:"))

# function definition
def make_twice():
    # local scope
    print('lets make it twice:')
    print(my_num*2)

# global scope
print(f"Your number is:{my_num}")

# function call
make_twice()

✅ Output:

please enter number:123
Your number is:123
lets make it twice:
246

2) Local variable:

Variables which are defined inside the function and only accessible inside the function are local variables.

  • Local variables can not be accessed or used outside the function.

  • Their scope is limited to the function only.

  • If you try to use local variables outside the function, it will provide NameError.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Lets define global variable my_num
my_num=int(input("please enter number:"))

# function definition
def multiply():
    # --- local scope ---
    loc_num = 100
    print('lets multiply with local variable:')
    print(my_num*loc_num)

# --- global scope ---
# function call
multiply()

# lets print local both varibales in global scope
print(f"global variable is:{my_num}")
print(f"local variable is:{loc_num}")

✅ Output:

please enter number:123
lets multiply with local variable:
12300
global variable is:123
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 ~\AppData\Local\Temp/ipykernel_10480/3249250737.py in <module>
    15 # lets print local both varibales in global scope
    16 print(f"global variable is:{my_num}")
---> 17 print(f"local variable is:{loc_num}")
    18

NameError: name 'loc_num' is not defined
---

Global Keyword:

What if we want to modify the global variable inside local scope i.e. inside the function?

  • Yes we can modify the value of global variable inside the function using global keyword.

  • We can not modify the global variable directly inside local scope by direct value assignment.

  • We can also declare global variable inside the function using global keyword.

Lets try to modify the global variable my_num inside the function by direct value assignment.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Lets define global variable my_num
my_num=int(input("please enter number:"))

# function definition
def twice():
    print(' --- local scope ---')
    # update the global variable
    my_num = 150
    print(f"global variable in local scope:{my_num}")
    print(f'lets make it twice:{my_num*2}')


# function call
twice()
print(' --- global scope ---')
# lets print global varibale
print(f"global variable in global scope:{my_num}")

✅ Output:

please enter number:123
--- local scope ---
global variable in local scope:150
---
lets make it twice:300
--- global scope ---
global variable in global scope:123
---

Lets use global keyword to update the global variable inside local scope.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Lets define global variable my_num
my_num=int(input("please enter number:"))

# function definition
def twice():
    print(' --- local scope ---')
    # update the global variable
    global my_num
    my_num = 150
    print(f"global variable in local scope:{my_num}")
    print(f'lets make it twice:{my_num*2}')


# function call
twice()
print(' --- global scope ---')
# lets print global varibale
print(f"global variable in global scope:{my_num}")

✅ Output:

please enter number:123
--- local scope ---
global variable in local scope:150
---
lets make it twice:300
--- global scope ---
global variable in global scope:150
---

Lets declare global variable inside local scope using global keyword.

💻 Example:

1
2
3
4
5
6
7
8
9
def create_variable():
    # --- declare global variable inside local scope ---
    global x
    x="Hello World"

# function call
create_variable()
# access variable from global scope
print(x)

✅ Output:

Hello World

Here are the quick notes on above topic:


Be prepared with below questions prior to your interview !

Frequently asked Interview Questions
  1. What is scope of a variable in Python?

  2. What are types of variable scopes?

  3. What is a global variable?

  4. What is a local variable?

  5. Can you access a global variable within a function?

  6. Can you modify a global variable within a function?