scope of variable in python example

Hello Students Welcome to our new python blog. In this blog i am going to tell you scope of variable in python in details. In this blog you will also learn types of scope in python. Here we will learn global scope in python and local scope in python in details. So must check out the full article

Scope Of Python Variable

The scopes of a variable are the rules that determine in which part(s) of the program that variable is identified and can be accessed. There are two kinds of scope of variable in Python: Global Scope and Local Scope. Which variables are global and local? How? When? Where? Why? can global and local variable be created and used, is all part of the discussion in this chapter. While reading this chapter you will learn about LEGB rule, followed by computer to search a variable used in the program. At the end this chapter explains the process of creating our own modules of functions and module functions: dir() and reload().

Types of scope in python

The scopes of a variable in programming language are the rules that determine in which part(s) of the program that variable is identified and can be accessed. There are two kinds of scope of variable in Python:

  1. Global Scope 
  2. Local Scope

1) Global Scope or global variable python

A variable declared in top most level segment (_main_) of the program is said to have global scope. In other words any variable which is created and initialized in main program and not i any function or other block is having global scope. Any variable which is created with global scope is accessible within the whole program including all the functions and blocks of the program.

2) Local Scope or local variable python

A variable created and initialized within a function body is said to have local scope i.e. the variable created inside the function can be used or accessed only within that function. The local variables are not known outside the function in which they are created. The following program demonstrate the concept of local and global scope.

python local and global variable examples

Consider another example of Python program that explain how global variable are accessible in the whole program and error generated when local variables are used outside the function.

python modules create and import in python

Python user defined functions with examples(2024)

Write a program to input three numbers. Calculate and print average numbers using user define function.

def avrg():

           avg=(a+b+c)/3

          print(“Average”, avg)

#main program

a=int(input(“Enter First number :”))

b=int(input(“Enter Second number :”))

c=int(input(“Enter Third number :”))

avrg()

print(“Average=”, avg)#error

Local and Global variable with same name

If in a function we create a variable by assigning it a values, using the name which is already present in global environment then Python creates a new variable in its local scope and does not uses its higher scope variable. Following program demonstrate this concept of hiding global variable when local variable is created with same name.

def func1():

               k=10

               print(“K=”,k)#10

#main program

k=25—-Global

print(“K=”,k)#25

func1()

print(“K=”, k)#25

global variable in python example

def test():

k=15

print(“Values in Function…”)

print(“K=”,k)#15

print(“M=”,m)#10

#main program

k=25

m=10

print(“Values in main before calling Function…”)

print(“K=”k)#25

print(“M=”,m)#10

test()

print(“Values in main after calling Function…”)

print(“K=”k)#25

print(“M=”,m)#10

Difference between python local and global variable

Python local variable

  • The variable created or declared inside the function is called local variable.
  • local variable can not be used outside the function.
  • The scope of local variable is only within the function.
  • If we declare variable like a=10 than we can use this value inside the function if we print the value of a outside the function then we will get the error ( a is not defined)
  • If we declare local and global variable with same name then function always takes priority to local variable.

Python global variable

  • The variable created or declared outside the function is called global variable.
  • Global variable can be used inside or outside the function.
  • The scope of global variable is within the function and also outside the function.
  • If we declare variable like a=10 than we can print this value inside the function as well as  of a outside the function.
  • If we declare local and global variable with same name then global scope always takes priority to global variable.

Subscribe Our YouTube Channel For Live Class

https://www.youtube.com/@olevelguruji

Also Check Our Latest Uploads

Python library function examples

What is a dictionary in Python?

dictionary functions in python with examples (2024)

Leave a Comment