
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to use a global variable in a Python function?
There are 2 types of variables in python namely Local variables and Global variables. Local variables mean the variables that are declared inside a function or inside a method whose impact or scope is only present inside that specific block and it doesn’t affect the program outside that block.
Global variables mean the variables that are declared outside any function or method and these variables have an impact or scope through the entire program.
We can also instantiate global variables inside a function by using a global keyword, if we want to declare global variables outside a function then we may not need to use a global keyword.
If a variable with the same name globally and locally then inside the function where the local variable is declared the local value is used and the global value is used elsewhere.
Example 1
Let us see an example for a global variable in python −
a = 5 def local(): a = 3 print("Value of local variable a is ",a) local() print("Value of global variable a is ",a)
Output
('Value of local variable a is ', 3) ('Value of global variable a is ', 5)
Example 2
Following is another example for this −
a = 5 def globalV(): print("The value of a is ",a) globalV()
Output
('The value of a is ', 5)
Example 3
In the following example we are defining two global variables after the function −
def product(): return a * b a = 10 b = 5 print(product())
Output
50
Example 4
Now let us let us try to create a global variable with in a function using the “global” keyword −
def func(): global a a = 7 func() b = 5 add = a + b print(add)
Output
12
Example 5
Following example shows how the global variable is accessed both inside and outside the function sample.
# This function uses global variable k k = "I like green tea" def sample(): print k #accessing global variable inside function sample() print k #accessing global variable outside function
Output
I like green tea I like green tea
- Related Articles
- How to define global variable in a JavaScript function?
- How to declare a global variable in Python?
- How to use variable-length arguments in a function in Python?
- How to use Global Variable in Postman Request?
- How to create and use Global Variable in swift
- How do I declare a global variable in Python class?
- How to declare a global variable in C++
- How to declare a global variable in PHP?
- How to set a global variable in Postman?
- How to create a Global Variable in Postman?
- How to change the value of a global variable inside of a function using JavaScript?
- Create global variable in jQuery outside document.ready function?
- How to detect whether a Python variable is a function?
- How to use variable number of arguments to function in JavaScript?
- How to use a variable in a string in jQuery?
