PYTHON DICTIONARY
CommentsData typesVariablesIf ElseIf Elif ElseFunctionsFor loopWhile loopData structuresString slicing/indexingUser defined functionLocal/Global variableValidation and TryMethodsFile I/OPseudocodeModules |
What is a Local/Global variable?Python challenge - Local vs Global variableEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Try the code above, does it display the value of x on the screen? Try it again but change the print function to print(a) or print(b). What happens? The "x" variable has "global" scope while the "a" and "b" variables are local to their respective user defined functions.
Python challenge - Local vs Global variableMake the following change to the code by adding the line "global a". In this line, you are changing the scope of the variable from local to global. Make the variable "b" global and then print it's value.
|