Python Global Variables

In programming language, variables are used to store information. For example, in developing a student management software system, the name, email, and age of a student will be stored in the respective variables. Like other programming languages, Python has both global and local variables. In Python, global variables are declared outside of the function and can be used everywhere in the program. This article explains global variables in Python in detail with some examples.

The scope of the global variable is very wide, and it is not limited to any specific function. These variables can be used both inside and outside of the function for storing and retrieving information.

Creating and Using Global Variables

In this example, we will creayte and use a global variable in our Python script.

#creating a name variable
name = "kamran"
#creating a function
def student():
    print("The name is: ",name)
#calling the function
student()

Output

In the given code, the ‘name’ variable is declared, and the value is assigned outside the function. Next, we called and used this variable inside the function. The student function prints the value of the name of the student, and the name value is taken by the global variable.

Now, if we declare the same ‘name’ variable inside the function and print it, the function’s local variable will be called and its value will be printed. Local variables are variables that are created and used inside a function.

#creating a name variable
name = "kamran"
#creating a function
def student():
    #creating the local variable
    name = 'Talha'
    print("The name is: ",name)
#calling the function
student()

Output

The global variable is a great tool to store information throughout a program. When you need any information, you can call these variables inside any function and use their value. In this next example, we will create two global variables, num1 and num2, and use them in multiple functions for performing addition, subtraction, multiplication, and division.

#creating two global variables, num1 and num2
num1 = 10
num2 = 20
#creating addition function
def addition():
    print("The sum is: ",num1+num2)
#creating subtraction function
def subtraction():
    print("The difference is", num2-num1)
#creating multiplication function
def multiplication():
    print("The multiplication value is: ",num1*num2)
#creating multiplication function
def division():
    print("The division value is", num2/num1)
#calling all functions
addition()
subtraction()
multiplication()
division()

Output

So far, we have only seen examples in which global variables were created outside the function. However, with the global keyword, we can create global variables within a function.

In and outside the function, the value of the variable created with the global keyword will remain the same.

#creating a function
def student():
    #creating the local variable
    global name
    name = "Kamran"

#calling the function
student()
print("The name is: ",name)

Output

Conclusion

This article explained global variables in Python with several examples. Variables are the building blocks of programming languages. They are used to store pertinent information. With global variables, the value remains the same throughout the program.



from Linux Hint https://ift.tt/33zZwIJ

Post a Comment

0 Comments