- 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
Global vs Local variables in Python
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.
#!/usr/bin/python total = 0; # This is global variable. # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2; # Here total is local variable. print "Inside the function local total : ", total return total; # Now you can call sum function sum( 10, 20 ); print "Outside the function global total : ", total
When the above code is executed, it produces the following result −
Inside the function local total : 30 Outside the function global total : 0
- Global and Local Variables in Python?
- Global and Local Variables in C#
- Global and Local Variables in Java
- System variables vs Local Variables in MySQL?
- Member variables vs Local variables in Java
- What are local variables and global variables in C++?
- User-defined variables vs Local Variables in MySQL?
- What is the difference between global and local variables in Python?
- How are C++ Local and Global variables initialized by default?
- Global variables in Java
- Global and Local Inversions in C++
- Local variables in Java
- Global Scope Variables in Postman?
- Global Variables in Lua Programming