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 ▸

How can you take a list of numbers and add 3 to every number in the list using only one line of code?


What can be used to iterate through a list in python?


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


Define a function that takes in the age of the user and adds it to an output sentence such as "You are ..... old".