- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use a global variable in a Python function?
The terms, global and local correspond to a variable's reach within a script or program. A global variable is one that can be accessed anywhere. A local variable can be accessed only within its frame. A local variable cannot be accessed globally.
Global variables are the one that are defined and declared outside a function and can be used anywhere.
If a variable with same name is defined inside the scope of a function then it will print the value given inside the function only and not the global value.
The given code is re-written to show how the global variable is accessed both inside and outside the function foo.
# This function uses global variable k k = "I like green tea" def foo(): print k #accessing global variable inside function foo() print k #accessing global variable outside function
C:/Users/TutorialsPoint1/~.py I like green tea I like green tea
- How to declare a global variable in Python?
- How to use Global Variable in Postman Request?
- How to use variable-length arguments in a function in Python?
- How to create and use Global Variable in swift
- 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 do I declare a global variable in Python class?
- Create global variable in jQuery outside document.ready function?
- How to use a variable in a string in jQuery?
- How to use global variables in Ruby?
- Redeclaration of global variable in C