global names may be referenced within function without being declared in other wordsglobal allows us to change names that live outside def at the top level of module file as we'll see laterthe nonlocal statement is almost identical but applies to names in the enclosing def' local scoperather than names in the enclosing module the global statement consists of the keyword globalfollowed by one or more names separated by commas all the listed names will be mapped to the enclosing module' scope when assigned or referenced within the function body for instancex global def func()global global xoutside def func(print(xprints we've added global declaration to the example heresuch that the inside the def now refers to the outside the defthey are the same variable this timeso changing inside the function changes the outside it here is slightly more involved example of global at workyz def all_global()global global variables in module declare globals assigned no need to declare yzlegb rule herexyand are all globals inside the function all_global and are global because they aren' assigned in the functionx is global because it was listed in global statement to map it to the module' scope explicitly without the global herex would be considered local by virtue of the assignment notice that and are not declared globalpython' legb lookup rule finds them in the module automatically alsonotice that does not even exist in the enclosing module before the function runsin this casethe first assignment in the function creates in the module program designminimize global variables functions in generaland global variables in particularraise some larger design questions how should our functions communicatealthough some of these will become more apparent when you begin writing larger functions of your owna few guidelines up front might spare you from problems later in generalfunctions should rely on arguments and return values instead of globalsbut need to explain why by defaultnames assigned in functions are localsso if you want to change names outside functions you have to write extra code ( global statementsthis is delibthe global statement