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
Answered by Peter C. Python tutor

1263 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

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


What is the outcome of the following code? print(2*[3,4,5])


Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8


How would you write a while loop to print all even numbers from 1-10?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

MyTutor is part of the IXL family of brands:

© 2026 by IXL Learning