Explain the difference between local and global variables

The difference is the scope. A local variable can only be seen within its scope, for example a variable defined in a method can only be seen within this method. However, a global variable can be seen by everything within the program.y = 1 def foo(): x = 5 def bar(): print(x) # Doesn't work print(y) # Prints 1

PC

Related Python Mentoring answers

All answers ▸

Write a function that checks whether a number is prime or not.


Write a program that can convert between celcius and farenheit temperature scales


What is the difference between a FOR loop and WHILE loop and how do I write them?


Print "Hello World!" ten times without typing (or pasting) the print function more than once