🌐
W3Schools
w3schools.com › python › python_scope.asp
Python Scope
A variable created in the main body of the Python code is a global variable and belongs to the global scope.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-scope-of-variables
Python Scope of Variables - GeeksforGeeks
July 12, 2025 - Unlike other languages like C/C++/JAVA, ... is created the moment we first assign a value to it. The location where we can find a variable and also access it if required is called the scope of a variable....
🌐
Real Python
realpython.com › python-scope-legb-rule
Python Scope and the LEGB Rule: Resolving Names in Your Code – Real Python
July 16, 2026 - The four scope levels—local, enclosing, global, and built-in—each control variable visibility in a specific context. Common scope-related built-in functions include globals() and locals(), which provide access to global and local namespaces.
🌐
Readthedocs
python-textbok.readthedocs.io › en › 1.0 › Variables_and_Scope.html
Variables and scope — Object-Oriented Programming in Python 1 documentation
Not all variables are accessible ... how long it exists depend on how it is defined. We call the part of a program where a variable is accessible its scope, and the duration for which the variable exists its lifetime....
🌐
Python Academy
python-academy.org › course › scope of variables
Scope of variables — Interactive Python Course
The rule Python uses to decide which variable a name refers to at any given point in the code is called variable scope.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module2_EssentialsOfPython › Scope.html
Scope — Python Like You Mean It
The scope of a variable refers to the context in which that variable is visible/accessible to the Python interpreter.
🌐
DataFlair
data-flair.training › blogs › python-variable-scope
Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair
April 13, 2026 - Variables written outside all functions sit in the global scope. Any function can read them, but changing them inside a function needs the global keyword. Using too many globals can confuse larger programs. Let’s take an example, but before, let’s revise Python syntax.
🌐
Reddit
reddit.com › r/learnpython › how does scope in python work?
r/learnpython on Reddit: How Does Scope in Python Work?
January 18, 2021 -

Trying to understand how scope works in Python. It seems to work a bit differently than in other languages like C++ and Java. When I run the code below

def foo():
    y = 5

x = 2
foo()
print(x+y)

it returns a name not defined error. That's expected since the scope of y disappeared before it could be used. However, when I run the following code

x = 2
if True:
    y = 3
print(x+y)

It works just fine. The program identifies y even though it is in another scope. When trying out the same thing in other languages like C++

int x = 2;
if (true)
{
    y = 2;
}
cout<<x + y<<endl;

it gives a not declared in this scope error. That's what I expected in Python but clearly that's not the case. Python somehow keeps track of variables inside the scope of an if statement or loop even after it exits and the variables were not declared outside of it. Can anyone explain how scope in Python differs from scope in C++ or Java? How does Python treat scope differently?

Find elsewhere
🌐
Medium
dhirajkumarblog.medium.com › python-variable-scope-a-comprehensive-guide-5cb08c4cf43b
Python Variable Scope: A Comprehensive Guide | by Dhiraj K | Medium
June 13, 2025 - At its core, scope refers to the region of a program where a variable is recognized and can be referenced. Python follows a relatively straightforward scoping rule known as LEGB, which stands for Local, Enclosing function locals, Global, and ...
🌐
Daily Dose of DS
blog.dailydoseofds.com › daily dose of data science › variable scope in python
Variable Scope in Python - by Avi Chawla
July 27, 2024 - Thus, when we access ANY variable, it is scope that entirely determines how the names will be looked up in our code. In Python, specifically, scope resolution has four levels.
🌐
Data Science for Everyone
matthew-brett.github.io › teaching › global_scope.html
Global and local scope of Python variables — Tutorials on imaging, computing and mathematics
The scope of a variable refers to the places that you can see or access a variable. If you define a variable at the top level of your script or module or notebook, this is a global variable: ... The variable is global because any Python function or class defined in this module or notebook, ...
🌐
DataCamp
datacamp.com › tutorial › scope-of-variables-python
Python Variable Scope And The LEGB Rule Explained | DataCamp
September 11, 2025 - Python resolves names by searching Local → Enclosing → Global → Built-in scopes. Understanding that order—and when to use global or nonlocal—eliminates common errors like NameError and UnboundLocalError. Keep variables in the smallest practical scope, avoid shadowing built-ins, and use closures deliberately.
🌐
Programiz
programiz.com › python-programming › global-local-nonlocal-variables
Python Variable Scope (With Examples)
In Python, we can declare variables in three different scopes: local scope, global, and nonlocal scope.
🌐
Tutorialspoint
tutorialspoint.com › python › python_variables_scope.htm
Python Variable Scope
The scope of a variable depends on where and how it is defined. In Python, a variable can have either a global or a local scope.
🌐
Daily Dose of DS
blog.dailydoseofds.com › daily dose of data science › variable scope: a fundamental programming concept that no python programmer must ignore
Variable Scope: A Fundamental Programming Concept That No Python Programmer Must Ignore
January 3, 2024 - Thus, when we access ANY variable, it is scope that entirely determines how the names will be looked up in our code. In Python, specifically, scope resolution has four levels.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python variable scopes
Python Variable Scopes
March 27, 2025 - Python doesn’t have a truly global scope that spans across all modules except for the built-in scope. The built-in scope is a special scope that provides globally available objects such as print, len, None, True, and False. Basically, the built-in and global variables exist everywhere inside a module.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 6-3-variable-scope
6.3 Variable scope - Introduction to Python Programming | OpenStax
March 13, 2024 - A variable's scope is the part of a program where the variable can be accessed. A variable created outside of a function has global scope and can be accessed anywhere in the program.
🌐
YouTube
youtube.com › watch
Python variable scope 🔬 - YouTube
Python variable scope tutorial example explained#python #variable #scope# ========================================================# scope = The region that a...
Published: January 25, 2021
🌐
Python Basics
pythonbasics.org › home › python basics › python scope of variables
Python Scope of Variables - pythonbasics.org
Variables have a certain reach within a program. A global variable can be used anywhere in a program, but a local variable is known only in a certain area (function, loop) Sometimes the word scope is used in projects: "its outside the scope of the project", meaning not included.