Python Scope

Python · Functions & OOP

Theory

Scope is the region of code where a name (variable) can be seen. Python looks names up using the LEGB rule: Local (inside the current function), Enclosing (inside any outer function, for nested functions), Global (top level of the module) and Built-in (names like len and print). It searches in that order and uses the first match.

Assigning to a name inside a function makes it local to that function by default, even if a global with the same name exists. To change the module-level variable instead, declare global x first; in the example fn() sets the global x to 20, so print(x) shows 20. For nested functions, nonlocal lets the inner function rebind a variable of the enclosing function. Reading a global without assigning to it needs no keyword.

Why we use it

Scope keeps functions independent: variables created inside one function cannot accidentally overwrite variables elsewhere. Knowing LEGB explains surprising errors such as UnboundLocalError.

When to use it

Keep variables local and pass values in through parameters and out through return. Use global sparingly (for constants or simple scripts) and nonlocal for closures; heavy use of globals makes code hard to test.

How it works

  1. Code uses the name x
  2. Is x local to this function? Yes: Use the local x. No: Check the enclosing function.
  3. Found in an enclosing function? Yes: Use the enclosing x. No: Check module globals.
  4. Found in globals? Yes: Use the global x. No: Check built-ins, else NameError.
  5. Value of x resolved

Syntax

global variable_name

Example

x = 10
def fn():
    global x
    x = 20

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic