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

1269 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a short program to print all the even numbers 1 to 100


Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .


Explain how you would write a python program that takes a rectangle and a point in a 2D space as command-line arguments and checks if they intersect.


What is the difference between & and && operators?


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