A variable can only be accessed in the area in which it is defined, which is called scope of the variable.
If a variable is created inside a function, it is called local variable and has local scope. A local variable can only be used inside that function.
In the below example, a local variable called MyString is created and used inside the function called MyPrint() to print it.
def MyPrint(): MyString = "Hello World!." print(MyString) MyPrint()
Hello World!.
A local variable can not be used outside the function in which it is defined but it can be used in any function inside that function.
In the below example, a local variable called MyString is created inside the function called MyFunction. Along with this, a function called MyPrint() is also defined inside MyFunction. The local variable MyString is available inside MyPrint().
def MyFunction(): MyString = "Hello World!." def MyPrint(): print(MyString) MyPrint() MyFunction()
Hello World!.
If a variable is created outside a function, it is called global variable and has global scope. A global variable can be used anywhere, inside the function and outside the function.
In the below example, a global variable called MyString is created and used inside the function called MyPrint() to print the global variable.
MyString = "Hello World!." def MyPrint(): print(MyString) MyPrint()
Hello World!.
If a variable with same name is created inside the function, it will be a local variable and can be used inside the function only. Any operation performed on local variable will not change the global variable.
In the below example, a global variable called MyString is created in the main body of the program. A local variable with the same name is also created inside the function called MyPrint(). This local variable can only be used inside the function MyPrint(), therefore the function uses the local variable when it is called in the program. Along with this, any operation performed on the local variable will not change the global variable, therefore when the variable MyString in printed outside the function, the variable takes global value only which is unaffected by the local variable.
MyString = "Hello World!." def MyPrint(): MyString = "Hello Python" print(MyString) MyPrint() print(MyString)
Hello Python Hello World!.
There are some reserved keywords in Python which cannot be used as variable name. Along with this, rules for creating Python variable name are listed below:
Please note that Python is a case-sensitive language. Hence, variables in Python are also case-sensitive.
In Python, a variable created inside a function has local scope and can only be used inside that function. To create a variable with global scope inside a function, Python global keyword is used. A global variable can be used anywhere, inside the function and outside the function.
In the below example, the global keyword is used to create a variable called MyString with global scope inside the function called MyFunction.
def MyFunction(): global MyString MyString = "Python" MyFunction() print("Learning", MyString ,"is fun.")
Learning Python is fun.
The value of a global variable, which is created outside of a function, can be changed inside the function by referring to the variable using the global keyword.
MyString = "Java" def MyFunction(): global MyString MyString = "Python" MyFunction() print("Learning", MyString ,"is fun.")
Learning Python is fun.