> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scopes

> This article explains variable scoping in Python, focusing on local and global variables and the use of the global keyword.

In this lesson, we explore how variable scoping works in Python. By understanding the difference between local and global variables, you can write clearer and more effective code. This guide covers how variables declared inside a function are confined to that function, and when the global keyword is required.

## Local Variables

Variables defined inside a function are local to that function. They cannot be accessed outside of the function's scope. Consider the following example:

```python theme={null}
def input_number():
    result = int(input("Enter a number: ")) * 100
    return result
```

Here, the variable `result` is local to the `input_number` function. Trying to reference `result` elsewhere in your code will lead to an error because its scope is strictly within the function.

## Global Variables and Shadowing

It is possible to use variables that are declared outside a function (global variables) within the function body. However, if a variable with the same name exists both globally and locally, the local variable will take precedence—a concept known as shadowing.

### Example of Variable Shadowing

```python theme={null}
num = 100

def input_number():
    num = 50  # Local variable shadows the global 'num'
    result = int(input("Enter a number: ")) * num
    return result
```

In this example, the function uses the local variable `num` (which is 50) instead of the global `num`.

### Example Using Global Variable

If no local variable is defined, the function will use the global variable:

```python theme={null}
num = 100

def input_number():
    result = int(input("Enter a number: ")) * num  # Uses global 'num'
    return result
```

Python first looks in the function's local scope; if it doesn't find the variable there, it then checks the global scope.

## Using the Global Keyword

By default, variables declared inside a function are not accessible outside of it. To modify a global variable within a function, you must declare it as global using the `global` keyword.

<Callout icon="lightbulb" color="#1CB2FE">
  Declaring a variable as global allows you to modify it inside a function, making it accessible in the global scope after the function call.
</Callout>

Here's an example:

```python theme={null}
num = 100

def input_number():
    global own_num
    own_num = 50  # 'own_num' is now a global variable
    result = int(input("Enter a number: ")) * own_num
    return result
```

After calling `input_number()`, you can access the variable `own_num` from the global scope:

```python theme={null}
>>> input_number()
2  # Example output based on user input
>>> print(own_num)
50
```

## Practice Exercises

Now that you understand local and global variable scopes in Python, try practicing these concepts with some hands-on exercises. Experiment with variable shadowing and the global keyword to see how scope affects your code.

For further learning, check out the following:

* [Python Official Documentation](https://docs.python.org/3/tutorial/)
* [Understanding Python Scoping](https://realpython.com/python-scope-namespace/)

That's it for this lesson. Happy coding!

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/python-basics/module/e169dc83-5d71-40ac-8437-4f500246efe6/lesson/3728e477-4577-4026-9942-36f7dbaa7f87" />
</CardGroup>
