Global and local variables in Python

07-12-22 Ahmed Obaid 2868 0

​Global or global variables are variables that are declared outside the function. global variables have a global scope. This means that they can be accessed from anywhere in the entire program, while local variables are those that are defined inside the function and their scope is limited inside the function only. In other words, local variables are only accessible inside the specified function. Global variables can be accessed throughout the program and within each function.

Python Variables

Global Variables

It is defined outside any function and can be accessed throughout the program, i.e. inside and outside each function. Let's see how to create a global variable.


# This function uses global variable name
name = "ahmed obaid"
def myfunc():
print("My Name is " + name)
myfunc()

The output will be:


My Name is ahmed obaid

In the previous example, we declared a global variable named name and then called it in the myfunc function.undefined The name variable can also be used both inside and outside the myfunc() function.

 use of the word ( global )

 When you create a variable inside a function, that variable is local, and can only be used inside that function.

 But the value of a global variable can be changed inside a specific function by adding the word (global) before the variable

 Example:


name = "ahmed obaid"
def myfunc():
global name
name = "hamza"
print("My Name is " + name)
myfunc()

 The output will be:


My Name is hamza

 In the previous example, we defined a global variable called name and assigned the value ahmed obaid to it. Then we create a variable with the same name name inside the myfunc function and change its value to hamza. In order for this to be done correctly, we put before it the word global and next to it the name of the variable name. This is so that we can change its value inside the function

 local python variables

Local variables are variables declared inside a function. These variables are known to have a local scope. This means that it can only be accessed from within the function in which it was created.

 Example:


def welcome():
# local variable
message = "Welcome to Ahmed Obaid's blog"

print('Local', message)
welcome()

 In the previous example, we created a function called welcom, and inside it we defined a local variable called message and set a value for it that is a welcome message "Welcome to Ahmed Obaid's blog" Then we called the welcome() function

 The output will be:


Local Welcome to Ahmed Obaid's blog

 What if we ask the program to print the local variable message outside the function as in the following example

 Example:


def welcome():
# local variable
message = "Welcome to Ahmed Obaid's blog"

# try to access message variable
# outside welcome() function
print(message)

 The output will be:


NameError: name 'message' is not defined

 In the previous example, we gave a print command to the local variable message outside the function, so we got an error message because the variable message was defined only inside the welcome function, so the program could not recognize it.

 more information

 You may wish to refer to the following resources for additional information on this topic

 Python documentation

 if undefined Did you have any questions? Leave it in the comments



Tags


Python python variables

Share page