DS 1501 · Lesson 19 of 24
Instructor: Corey Schafer20m
Sign in to watchMaster how Python resolves variable names across call frames using LEGB, when global modifies outer bindings, and how recursive functions solve self-similar problems. What you will learn: • Python LEGB resolution order: Local -> Enclosing -> Global -> Built-in. • Local shadowing vs global keyword and UnboundLocalError (reading a variable before local assignment inside a function). • Anatomy of recursion: base case (stopping condition), recursive step (shrinking subproblem), and call stack depth. • Tracing classic recursive examples by hand: factorial, Fibonacci tree, sum of list/digits, and string reversal. University Outline Mapping: Class 19 · CO3 · Reading: Chapter 4 (deitel) · Activities: Lecture, Slides, Demonstration.
Note
Outline: Class 19 | CO3 | Reading: Deitel Chapter 4 UnboundLocalError & Global Trace: x = 10 def bump(): global x # without this, x += 1 raises UnboundLocalError! x += 5 Recursion Checklist: (1) Smallest valid base case? (2) Does EVERY recursive call strictly move toward it? (3) Watch Reducible's 5-step recursion visual guide in Further reading.
Same resources as in the community course — sign in to watch the lesson and track progress.
Watch the whole course.
Sign in to play every lesson, track your progress, and ask questions alongside the community.
Already signed in? Open lesson in Community