0% found this document useful (0 votes)
208 views

5.global and Local Variable

Python local variables are defined inside functions and only accessible within those functions. Global variables are defined outside functions and accessible throughout the entire program. When a local variable has the same name as a global variable, the local variable takes precedence and the global variable is not accessible within that function. Local variables allow sections of code to be reusable by preventing variable values from being changed externally, while global variables can be accessed anywhere in a program.

Uploaded by

tanmayeesamal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

5.global and Local Variable

Python local variables are defined inside functions and only accessible within those functions. Global variables are defined outside functions and accessible throughout the entire program. When a local variable has the same name as a global variable, the local variable takes precedence and the global variable is not accessible within that function. Local variables allow sections of code to be reusable by preventing variable values from being changed externally, while global variables can be accessed anywhere in a program.

Uploaded by

tanmayeesamal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Global variables are those which are not defined inside any function and have a global

scope whereas Python local variables are those which are defined inside a function and their
scope is limited to that function only. In other words, we can say that local variables are
accessible only inside the function in which it was initialized whereas the global variables are
accessible throughout the program and inside every function.

Python Local Variables


Local variables in Python are those which are initialized inside a function and belong only to that
particular function. It cannot be accessed anywhere outside the function. Let’s see how to create
a local variable.

Creating local variables in Python


Defining and accessing local variables

def f():

# local variable
s = "I love Geeksforgeeks"
print(s)

# Driver code
f()
Output
I love Geeksforgeeks
Can a local variable be used outside a function?

If we will try to use this local variable outside the function then let’s see what will happen.

def f():

# local variable
s = "I love Geeksforgeeks"
print("Inside Function:", s)

# Driver code
f()
print(s)
Output:

NameError: name 's' is not defined


Python Global Variables
These are those which are defined outside any function and which are accessible throughout
the program, i.e., inside and outside of every function. Let’s see how to create a Python global
variable.

Create a global variable in Python


Defining and accessing Python global variables.

# This function uses global variable s


def f():
print("Inside Function", s)

# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)
Output
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
The variable s is defined as the global variable and is used both inside the function as well as
outside the function.

Note: As there are no locals, the value from the globals will be used but make sure both the
local and the global variables should have same name.

Why do we use Local and Global variables in Python?


Now, what if there is a Python variable with the same name initialized inside a function as well
as globally? Now the question arises, will the local variable will have some effect on the global
variable or vice versa, and what will happen if we change the value of a variable inside of the
function f()? Will it affect the globals as well? We test it in the following piece of code:

Example

If a variable with the same name is defined inside the scope of the function as well then it will
print the value given inside the function only and not the global value.

# This function has a variable with


# name same as s.
def f():
s = "Me too."
print(s)

You might also like