Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Scope of Variables

Python

python logo

In this tutorial we will learn about scope of variables in Python.

What is scope of a variable?

The variable we create in a Python program are not accessible everywhere. They have a scope which defines were we can access them.

There are two categories of variable scope.

  • Global variable scope
  • Local variable scope

What is a global variable?

Variables that are defined outside of any function are referred as global variables.

Global variables are accessible both inside and outside of a given function.

In the following Python program we are creating a global variable and accessing it both inside and outside a function.

# global variable
score = 10

print("global variable score:", score)

# func
def foo():
    print("value of score from inside foo():", score)

# calling foo
foo()

The above code will give us the following output.

global variable score: 10
value of score from inside foo(): 10

What is a local variable?

A variable created inside a function is referred as local variable.

We can't access local variable outside a function.

In the following Python program we are creating a local variable inside a function.

# func
def foo():
    x = 10
    print("local variable x:", x)

# function call
foo()

The above code will give the following output.

local variable x: 10

Trying to access local variable outside the function will raise an error.

Mask global variable inside function

We can mask global variable inside a function by creating a variable by the same name.

In the following Python program we are creating a global variable and then masking it inside a function by creating a local variable by the same name inside the function.

# global variable
x = 10
print("global variable x before function call:", x)

# func
def foo():
    # local variable
    x = 20
    print("local variable x inside foo():", x)

print("function call")
foo()

print("global variable x after function call:", x)

The above code will print the following output.

global variable x before function call: 10
function call
local variable x inside foo(): 20
global variable x after function call: 10

Change global variable from inside a function

As we saw earlier that if a variable inside a function shares the same name as a global variable then the local variable masks the global variable.

So, any change made to the local variable is not reflected back to the global variable as it is masked.

To modify the global variable from inside a function we have to use the global keyword and then work with the global variable.

In the following Python program we are changing the value of a global variable from inside a function.

# global variable
x = 10
print("global variable x before function call:", x)

# func
def foo():
    # get the global variable
    global x

    # now change the value
    x = 20
    print("global variable x inside foo():", x)

print("function call")
foo()

print("global variable x after function call:", x)

The above code will give us the following output.

global variable x before function call: 10
function call
global variable x inside foo(): 20
global variable x after function call: 20

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT