Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Scope and Namespace | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 12 of 55

·Python

Scope and Namespace

20 minutes

Course progress22%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • LEGB rule determines how Python looks up identifiers.
  • Local scope is inside a function; enclosing is inside a nested function.
  • Global scope is the module level; built‑ins are always available.
  • Use `global` or `nonlocal` to modify variables in outer scopes.

Scope and Namespace

Python follows the LEGB rule to resolve names: Local → Enclosing → Global → Built‑in. Understanding this helps you avoid accidentally shadowing variables.

count = 0  # Global

def outer():
    count = 1  # Enclosing
    def inner():
        count = 2  # Local
        print('inner count:', count)
    inner()
    print('outer count:', count)

outer()
print('global count:', count)

Running the script prints 2, 1, and 0, demonstrating each scope in action.

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonLambda FunctionsContinue to lesson 13Data Structures – ListsBack to Python Fundamentals curriculum