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 s(n) that will return a list of the first n squares.


Explain why creating a list of several instances of one element using something like my_list = [a] * 5 can result in strange behaviour


Write a Python script to take a product name as input and then automatically google search reviews for it and open the top 3 search results in different tabs


Write a simple Python program that asks a user to input their age and name and then return the text: "Hi my name is *NAME*, and I am *AGE* years old."