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 ▸

What is the difference between a for and a while loop?


How do you define a class in python


Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs


How do I check if a number is prime using a python program?