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 ▸

Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


What is the difference between using range() and a list of values?


Write a function that takes a string, and outputs that string formatted in camelcase. (alternating upper and lower case for each character, e.g. cAmElCaSe)