Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON FUNCTIONS / CHAPTER 04 / Learn / LESSON 03
Third class. Variables created inside a function are local — they live only while the function runs.
x = 10def bump():
x = 1
return xprint(bump()) # 1
print(x) # 10 — the outer x did not changeThe assignment x = 1 inside creates a new local x. That does not touch the global x unless you use global (we do not learn that today).
Common mistake: expecting an outer variable to change from an inner assignment; or reading a local before it exists.
Learn: a local assignment does not change the global x.
DECISION STANDBY
›