> For the complete documentation index, see [llms.txt](https://gyansetu-python.gitbook.io/python-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-python.gitbook.io/python-programming/functions-in-python/scope-of-variables.md).

# Local/Global Variables

#### Scope of Variables <a href="#scope-of-variables" id="scope-of-variables"></a>

All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.

The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python

1. Global variables
2. Local variables

#### Global vs. Local variables <a href="#global-vs.-local-variables" id="global-vs.-local-variables"></a>

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.

Example 1:

```python
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print("Inside the function local total : ", total)
   return total;

# calling sum function
sum( 10, 20 );
print("Outside the function global total : ", total)
```

Inside the function local total : 30

Outside the function global total : 0

Example 2:

```python
x = 10
def hey():
    s = x
    s = s + 5
    print("Local variable x = ",s)
print("Global x = ",x)
hey()
print("Global x = ",x)
```

Global x = 10&#x20;

Local variable x = 15&#x20;

Global x = 10

Example 3:

```python
x = 10
def chage():
    global x
    x = x + 5
    print("In function Value = ",x)
print("Before function calling x = ",x)
chage()
print("After function calling  x = ",x)
```

Before function calling x = 10&#x20;

In function Value = 15&#x20;

After function calling x = 15

Example 4:

```python
c = 1
def hello():
    global c
    if c == 10 :
        c = 1
        return None
    print("Hello World Times {}".format(c))
    c = c + 1
    hello()
    
hello()
```

Hello World Times 1&#x20;

Hello World Times 2&#x20;

Hello World Times 3&#x20;

Hello World Times 4&#x20;

Hello World Times 5&#x20;

Hello World Times 6&#x20;

Hello World Times 7&#x20;

Hello World Times 8&#x20;

Hello World Times 9

Using variables from a function inside another function

Example 5:

```python
def hello():
    c = 10 
    
    def hello1():
        global x
        c = c + 20
        print(c)
        
    hello1()
    
hello()
```

Above program will give error

**Nonlocal variables** are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope

Example 6:

```python
c = 100
def hello():
    c = 10 
    def hello1():
        nonlocal c
        c = c + 20
        print(c)
    hello1()
    print(c)
hello()
print(c)
```

30

30

100


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/functions-in-python/scope-of-variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
