i

Python Programming

Scope of Variables

The Variables in a program may not be accessible at all the locations inprogram. This depends on where you declared the variable.

The Scope of a variable determines portion of the program in where you get access the particular identifier. There are two types of variables in Python:

•    Global-variables

•    Local-variables

Global vs. Local-variables

The Variables that are defined inside a function body has a local scope, and those defined outside has a global-scope.

That means that local variables can be accessed only in the function in which they declared, where global variables can access throughout the program body by all functions. When you call the function, the variables declared inside it are come into scope.

Live Demo

Output:

Inside function local total :  30

Outside function global total :  0