# Scope

> Where in a program a name can be seen. Names in a function stay local.

**Field:** Software Development  
**Category:** Programming Concepts  
**Also known as:** Variable Scope, Local Variable, Global Variable

## Think of it like

Think of it like rooms in a house. A note left in the kitchen cannot be read from the bedroom.

## Definition

Scope is the part of a program where a name can be seen and used. A variable made inside a function is local: it only exists in that function. A variable made at the top of the file is global: the whole file can see it. When the function ends, its local names are gone. Keeping names local stops one part of the code from changing another by mistake.

## Example

Lucy makes total inside a function, then tries to print it outside. Python says total is not defined.

## Example code

```python
def f():
    total = 5

f()
print(total)
# NameError: name 'total' is not defined
```

Total lived only in the function. Outside, it does not exist.

## Why it matters

Scope keeps big programs safe to change. Two functions can each use a name like count without clashing.

## Common mistake

Using global variables for everything. Any line can change them, so bugs are hard to trace.

## Three principles

- **Local is safe.** Names inside a function stay there.
- **Global is shared.** Every part of the file can touch it.
- **Gone at the end.** Local names vanish when the function ends.

## Related terms

- [Variable](https://learn.benibix.com/term/development/variable)
- [Function](https://learn.benibix.com/term/development/function)
- [Parameter](https://learn.benibix.com/term/development/parameter)
- [Runtime Error](https://learn.benibix.com/term/development/runtime-error)

## Taught in

- [Scope and Passing Arguments](https://learn.benibix.com/learn/development-concepts/scope-and-passing-arguments): Programming Basics, 13/18

---

Source: https://learn.benibix.com/term/development/scope
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
