- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between global and local variables in Python?
A global variable is a variable that is accessible globally. A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.
Example
In the given code
q = "I love coffee" # global variable def f(): p = "Me Tarzan, You Jane." # local variable print p f() print q
Output
The output is as follows
Me Tarzan, You Jane. I love coffee
In the given code, p is a local variable, local to the function f(). q is a global variable accessible anywhere in the module.
- Related Articles
- What is the difference between global and local Variables in JavaScript?
- Global and Local Variables in Python?
- What are local variables and global variables in C++?
- What are the rules for local and global variables in Python?
- Global vs Local variables in Python
- What is the difference between Local Events and Global Events in jQuery?
- Difference Between Local and Global Variable
- Global and Local Variables in C#
- Global and Local Variables in Java
- Difference between static, auto, global and local variable in C++
- How are C++ Local and Global variables initialized by default?
- What is the difference between class variables and instance variables in Java?
- What is the difference between constants and variables?
- What is the difference between dynamic type variables and object type variables?
- What are class variables, instance variables and local variables in Java?

Advertisements