---
title: "Explain the Python Scope with example."  
description: "Explain the Python Scope with example."  
author: "ICSM Computer"  
published: 2025-10-05  
updated: 2025-10-07  
canonical: https://www.mindstick.com/interview/34386/explain-the-python-scope-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 5 minutes  

---

# Explain the Python Scope with example.

> **Scope** defines where in your code a variable can be accessed or modified.\
> In Python, every variable is only available within a certain part of the program — that part is called its **scope**.

### The 4 Types of Scope (LEGB Rule)

Python follows the **LEGB rule**, which stands for:

| Level | Name | Description |
| --- | --- | --- |
| **L** | **Local** | Inside a function or lambda — temporary and exists only while the function runs. |
| **E** | **Enclosing** | Inside nested functions — from an outer function that encloses the current one. |
| **G** | **Global** | Defined at the top level of a module or script — accessible throughout that file. |
| **B** | **Built-in** | Reserved Python names (like `len`, `print`, `sum`) — always available. |

| Keyword | Affects | Example Use |
| --- | --- | --- |
| `local` | Variables inside a function | `x = 5` inside a function |
| `enclosing` | Variables in outer function | Nested functions |
| `global` | Variables declared at the top level | `global x` |
| `built-in` | Reserved names | `print()`, `len()` |

### Example: Understanding Scope

```python
# Global scope
x = 10

def outer():
    # Enclosing scope
    y = 20

    def inner():
        # Local scope
        z = 30
        print("Inside inner function:")
        print("x (Global):", x)
        print("y (Enclosing):", y)
        print("z (Local):", z)

    inner()
    print("\nInside outer function:")
    print("x (Global):", x)
    print("y (Enclosing):", y)
    # z is not accessible here

outer()

print("\nOutside all functions:")
print("x (Global):", x)
# print(y)  # This will cause an error — y is not defined here
```

### Output:

```plaintext
Inside inner function:
x (Global): 10
y (Enclosing): 20
z (Local): 30

Inside outer function:
x (Global): 10
y (Enclosing): 20

Outside all functions:
x (Global): 10
```

### Modifying Variables Across Scopes

If you want to modify a variable from an outer scope, use:

1. `global` → to modify a **global** variable.
2. `nonlocal` → to modify an **enclosing** variable in a nested function.

## Example:

```python
count = 0  # global variable

def outer():
    total = 5  # enclosing variable

    def inner():
        nonlocal total
        global count
        total += 1
        count += 1
        print("Inside inner:", total, count)

    inner()
    print("Inside outer:", total, count)

outer()
print("Outside:", count)
```

### Output:

```plaintext
Inside inner: 6 1
Inside outer: 6 1
Outside: 1
```

###

## Answers

### Answer by ICSM Computer

> **Scope** defines where in your code a variable can be accessed or modified.\
> In Python, every variable is only available within a certain part of the program — that part is called its **scope**.

### The 4 Types of Scope (LEGB Rule)

Python follows the **LEGB rule**, which stands for:

| Level | Name | Description |
| --- | --- | --- |
| **L** | **Local** | Inside a function or lambda — temporary and exists only while the function runs. |
| **E** | **Enclosing** | Inside nested functions — from an outer function that encloses the current one. |
| **G** | **Global** | Defined at the top level of a module or script — accessible throughout that file. |
| **B** | **Built-in** | Reserved Python names (like `len`, `print`, `sum`) — always available. |

| Keyword | Affects | Example Use |
| --- | --- | --- |
| `local` | Variables inside a function | `x = 5` inside a function |
| `enclosing` | Variables in outer function | Nested functions |
| `global` | Variables declared at the top level | `global x` |
| `built-in` | Reserved names | `print()`, `len()` |

### Example: Understanding Scope

```python
# Global scope
x = 10

def outer():
    # Enclosing scope
    y = 20

    def inner():
        # Local scope
        z = 30
        print("Inside inner function:")
        print("x (Global):", x)
        print("y (Enclosing):", y)
        print("z (Local):", z)

    inner()
    print("\nInside outer function:")
    print("x (Global):", x)
    print("y (Enclosing):", y)
    # z is not accessible here

outer()

print("\nOutside all functions:")
print("x (Global):", x)
# print(y)  # This will cause an error — y is not defined here
```

### Output:

```plaintext
Inside inner function:
x (Global): 10
y (Enclosing): 20
z (Local): 30

Inside outer function:
x (Global): 10
y (Enclosing): 20

Outside all functions:
x (Global): 10
```

### Modifying Variables Across Scopes

If you want to modify a variable from an outer scope, use:

1. `global` → to modify a **global** variable.
2. `nonlocal` → to modify an **enclosing** variable in a nested function.

## Example:

```python
count = 0  # global variable

def outer():
    total = 5  # enclosing variable

    def inner():
        nonlocal total
        global count
        total += 1
        count += 1
        print("Inside inner:", total, count)

    inner()
    print("Inside outer:", total, count)

outer()
print("Outside:", count)
```

### Output:

```plaintext
Inside inner: 6 1
Inside outer: 6 1
Outside: 1
```

###


---

Original Source: https://www.mindstick.com/interview/34386/explain-the-python-scope-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
