code in the global scopeoutside of all functionscannot use any local variables howevercode in local scope can access global variables code in function' local scope cannot use variables in any other local scope you can use the same name for different variables if they are in different scopes that isthere can be local variable named spam and global variable also named spam the reason python has different scopes instead of just making everything global variable is so that when variables are modified by the code in particular call to functionthe function interacts with the rest of the program only through its parameters and the return value this narrows down the number of lines of code that may be causing bug if your program contained nothing but global variables and had bug because of variable being set to bad valuethen it would be hard to track down where this bad value was set it could have been set from anywhere in the programand your program could be hundreds or thousands of lines longbut if the bug is caused by local variable with bad valueyou know that only the code in that one function could have set it incorrectly while using global variables in small programs is fineit is bad habit to rely on global variables as your programs get larger and larger local variables cannot be used in the global scope consider this programwhich will cause an error when you run itdef spam()eggs spam(print(eggsif you run this programthe output will look like thistraceback (most recent call last)file " :/test py"line in print(eggsnameerrorname 'eggsis not defined the error happens because the eggs variable exists only in the local scope created when spam(is called once the program execution returns from spamthat local scope is destroyedand there is no longer variable named eggs so when your program tries to run print(eggs)python gives you an error saying that eggs is not defined this makes sense if you think about itwhen the program execution is in the global scopeno local scopes existso there can' be any local variables this is why only global variables can be used in the global scope