In most programming languages, two types of variables can be defined. These are global and local variables. Variables defined outside a function are called global variables. This variable is available and can be modified anywhere in the script. A variable declared within a function is called a local variable. However, if the same variable name exists as both a global and a local variable, the variable of the function has priority and the value of the global variable is ignored. The volume of the global variables in the Python script is shown in this lesson using several examples.

Example 1: Simple use of global variables

The following example shows an easy use of a global variable inside and outside the function. A global variable called text is used here to assign string data. To produce a global variable value, a user-defined function called printMessage() is defined. The value of the text variable is executed within the function and after changing the value outside the function.

#!/usr/bin/env python3

# Set the global variable
text = Welcome to LinuxHint

# Define function
def printMessage()

# Print the global variable of function
(global variable in function: n, text)

# Call function
printMessage()

# Change the value of the global variable
text = Learn Python programming

# Print the current value of the global variable
(global variable from function: n, text)

A way out:

After executing the script, the following output appears. It returned the Welcome to LinuxHint value assigned to the variable before the function is called. After changing the value of the variable, he printed the value of Learn Python Programming.

Understand the scope of global variables in python – Linux Hint

Example 2: Use of global and local variables of the same name

If you want to declare a variable name in a function that was previously declared as a global variable in Python, and you want to use this variable, the variable will be treated as a local variable and an error will be generated. The following script shows an error. Here the text variable is defined as global and local.

#!/usr/bin/env python3

# Set global variable
text = I like Python

# Definition of function
def printMessage():
print(text)

text = I love PHP

# Call function
printMessage()

A way out:

After executing the script, the following output appears.

Understand the scope of global variables in python – Linux Hint

To avoid the situation described above and if you want to declare a local variable with the same name as the global variable, you must first assign a local variable within a function. The following script shows that changes in the local variable do not lead to changes in the global variable. The text variable is executed here inside and outside the function.

#!/usr/bin/env python3

# Set global variable
text = I like Python

# Definition of function
def printMessage():
# Determines the local value
text = PHP
I like it # Print local variable, text
print(Value of ‘text’ in function: n, text)

# Call function
printMessage()

# Global variable, printable text
print (value of ‘text’ outside function: n, text)

A way out:

After executing the script, the following output appears.

Understand the scope of global variables in python – Linux Hint

Example 3: Use of global variables in function

The previous example shows that the global variable is not available in the function. You can use the global keyword to access the global variable from any function. The following script defines the cal_percentage() function to calculate the percentage of any number using both global and local variables. Here num is a global variable and perVal a local variable. The global keyword is used here to identify the global variable within a function, and the value of the global variable changes within a function.

#!/usr/bin/env python3

# Take the integer value
num = int (input: ))

# Define function
def cal_percentage():
# Identify the global variable with the global keyword
– global number.

# Take the integer percentage value
perVal = int (input: ))

# Calculate percentage
Result = float((number * perVal)/100)

# Print the formatted result
(%d %d = %f % (perVal, number, result))

# Change the value of the global variable
num = 500

# Print the value of the global variable for calling the function
print(nThe value of num = %d %num)

# Call function
cal_percentage()

# Output of the value of the global variable after calling the function
print(nThe numerical value = %d %num)

A way out:

After executing the script, the following output appears. Here 10% of 350 are counted and printed.

Understand the scope of global variables in python – Linux Hint

Example 4: Use of global variables from another script

The following example shows how a global variable declared in one scenario can be used in another scenario Suppose the global6.py filename uses global variables defined in the global5.py filename. Three global variables were reported in global5.py. To use variables in another script, you need to import globally5. The global6.py script calculates salaries for three types of employees based on input values and global variables.

/usr/bin/env python3
# is a global5.py file

#
Base salary = 5,000
Medical = 500
Transport = 500

/usr/bin/env python3
# is a global6.py file

# import global variables from global5.py
import global5

# Take the name of the employee’s post = entry (Fill in the name of the employee: )# Take the name of the employee’s post = entry (Fill in the name of the employee: )

# Initialization of error variables
Error = False

# Check the position and set the base salary to
if (post.upper() == CEO):
base = global5.Basic + (global5.Basic*0.4)
elif (post.upper() == MANAGER):
base = global5.Basic + (global5.Basic*0.25)
elif (post.upper() == CALCULATION) :
Base = global5.base + (global5.base*0.15)
other:
# Set the error to true if the item value is not found
Error = true

# Wage pressure as bottle
Error if (Error == False) :
Salary = basic + global5.medical + global5.transport
print (salary %s is %d %(name, salary))
other: print
(function not found)

A way out:

After executing the script, the following output appears. Here the script is executed twice with one valid and one invalid position.

Understand the scope of global variables in python – Linux Hint

Conclusion:

The concept of the global variable on Python is explained in this tutorial with some simple examples for new Python users. I hope that after reading this manual readers have the right knowledge of both global and local variables.namespace in python,which file contains the default environment variables when using the bash shell,where environment variables are stored in linux,global variables in linux,bash script environment variables,enclosing scope python,global variables python,environment variables in linux