What are local variables and global variables in Python?

Local variables are declared inside a function and have a scope that is confined to that function alone, whereas global variables are defined outside of any function and have a global scope. To put it another way, local variables are only available within the function in which they were created, but global variables are accessible across the programme and throughout each function.

Local Variables

Local variables are variables that are created within a function and are exclusive to that function. Outside of the function, it can’t be accessed.

Global Variables

Global variables are variables that are defined outside of any function and are available throughout the programme, that is, both inside and outside of each function.