Home | | Computer Science 12th Std | Scope of Variables

Python - Scope of Variables | 12th Computer Science : Chapter 7 : Core Python : Python Functions

Chapter: 12th Computer Science : Chapter 7 : Core Python : Python Functions

Scope of Variables

Scope of variable refers to the part of the program, where it is accessible, i.e., area where you can refer (use) it.

Scope of Variables

 

Scope of variable refers to the part of the program, where it is accessible, i.e., area where you can refer (use) it. We can say that scope holds the current set of variables and their values. We will study two types of scopes - local scope and global scope.

 

1. Local Scope

A variable declared inside the function's body or in the local scope is known as local variable.

Rules of local variable

·        A variable with local scope can be accessed only within the function/block that it is created in.

·        When a variable is created inside the function/block, the variable becomes local to it.

·        A local variable only exists while the function is executing.

·        The formate arguments are also local to function.

 

Example : Create a Local Variable

def loc():

y=0 # local scope

print(y)

loc()

Output:

0

 

Example : Accessing local variable outside the scope

def loc():

y = "local"

loc()

print(y)

When we run the above code, the output shows the following error:

The above error occurs because we are trying to access a local variable ‘y’ in a global scope.

NameError: name 'y' is not defined

 

2. Global Scope

A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block.

Rules of global Keyword

The basic rules for global keyword in Python are:

·        When we define a variable outside a function, it’s global by default. You don’t have to use global keyword.

·        We use global keyword to read and write a global variable inside a function.

·        Use of global keyword outside a function has no effect

Use of global Keyword

Example : Accessing global Variable From Inside a Function

c = 1                                       # global variable

def add():

print(c)

add()

Output:

1

Example : Modifying Global Variable From Inside the Function

c = 1                                 # global variable

def add():

c = c + 2 # increment c by 2

print(c)

add()

Output:

Unbound Local Error: local variable 'c' referenced before assignment

Note

Without using the global keyword we cannot modify the global variable inside the function but we can only access the global variable.

Example : Changing Global Variable From Inside a Function using global keyword

x = 0 # global variable

def add():   

global x      # increment by 2

x = x + 5    

print ("Inside add() function x value is :", x)

add()

print ("In main x value is :", x)

Output:

Inside add() function x value is : 5

In main x value is : 5

In the above program, x is defined as a global variable. Inside the add() function, global keyword is used for x and we increment the variable x by 5. Now We can see the change on the global variable x outside the function i.e the value of x is 5.

 

3. Global and local variables

Here, we will show how to use global variables and local variables in the same code.

Example : Using Global and Local variables in same code

x=8   # x is a global variable  

def loc():

global x

y = "local"

x = x * 2

print(x)

print(y)

loc()

Output:

16

Local

In the above program, we declare x as global and y as local variable in the function loc().

After calling the function loc(), the value of x becomes 16 because we used x=x * 2.

After that, we print the value of local variable y i.e. local.

Example : Global variable and Local variable with same name

x = 5

def loc():

x = 10

print ("local x:", x)

loc()

print ("global x:", x)

Output:

local x: 10

global x: 5

In above code, we used same name ‘x’ for both global variable and local variable. We get different result when we print same variable because the variable is declared in both scopes, i.e. the local scope inside the function loc() and global scope outside the function loc().

The output :- local x: 10, is called local scope of variable.

The output:- global x: 5, is called global scope of variable.

 

Tags : Python , 12th Computer Science : Chapter 7 : Core Python : Python Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 7 : Core Python : Python Functions : Scope of Variables | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.