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

1013 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Why are negative indexes used?


Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.


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


What is the difference between a list and an array?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences