It is strange to talk here about variables but they are in a very close relation with functions. The scope of a variable denotes where the variable is accessible within our program. There are two kinds of variables: local variables and global variables.
A local variable is accessible only in the function it is defined. A global variable is accessible anywhere within the program and it is not declared within a function.
global_1 = 'I am a global variable' def func_1(): print(global_1) local_1 = 'I am a local variable' print(local_1) func_1()
global_1 variable is declared outside of the function
func_1 so it can be accessible anywhere within the program. The
local_1 variable can be accessed just by the function
Let’s modify the code and try to access the variable
local_1 from another function.
global_1 = 'I am a global variable' def func_1(): print(global_1) local_1 = 'I am a local variable' print(local_1) func_1() def func_2(): print(local_1) func_2()
When we run the code in terminal, the function
func_1 does what is supposed to do but when we get to the function
func_2 we get the error “local_1” is not defined.