Python Global and Local Variables
Tags: python, beginners, programming, tutorial
Published: 2022-02-20 10:20:32

> [Sign up to my newsletter!](https://baransel.dev/newsletter/).


In this lesson, we will learn the scope of Functions, namely **Global** and **Local** variables, scopes and namespace expressions. Let's start.

As in all programming languages, every class, function and variable has a scope. To put it simply, a block and namespace in which the variables we create are valid are stored in that namespace and are deleted from memory when you leave that block.

## Local Variable

Let's show it with a simple example;

```python
def function():
    a = 5
   
function()
print(a)
```

When we run the code, we get the following error.

```python
Traceback (most recent call last):
  File "/Users/baran/Documents/work/baransel.dev/functions.py", line 7, in <module>
    print(a)
NameError: name 'a' is not defined
```

So we got the error variable a could not be found because the scope of variable `a` is valid only inside the `function()`. Because variable `a` is a **local** variable. Variables defined for a function are specific to that function only.

## Global Variable

We learned what a **local** variable is, now let's look at what a **global** variable is. Let me just show you with an example.

```python
a = 3
def function():
    a = 5
    print(a)
function()
print(a)

# 5
# 3
```

When we run the code, we output `5` and `3`. Let's see how it works now;

When we run the program, since Python is an interpretive language, it reads line by line first, the `function()` will run, and as a result, we got the value of `5` in the function. After the `function()` is finished, the `print()` function will run, and as a result, we got the value `3`. Here, since the variable `a` in the `function()` function is a local variable, its scope is only within the function. The `print()` function cannot reach it and prints the **global** variable `a`. Then we can define **global** variables as follows;

> **Global** variables are variables that do not belong to a single function and can be called and used from anywhere in the program once they are defined.

Now a question mark has appeared in your mind. The **global** variable can be called and used everywhere, but can we call these variables inside the function? Of course, you can use **global** variables in a function, let me show you right away;

```python
a = 3
def function():
    print(a)
function()

# 3
```


## Global Expression

Now that we have learned this, there is something that I have just said to you so that you do not get confused. Just before, while we were defining global variables, we made a definition like this; we used the definition of global variables only variables that do not belong to a function. And we also used global variables inside the function. Can we do something like this, can we create a global variable inside the function? Let's try it now and see;

```python
def function():
    global a
    a = 3
    print(a)
function()
print(a)

# 3
# 3
```

As you can see, we got two values of `3`, so let's see how we got them.

First, `function()` will run and print the value of `a` in the function, then the `print()` function will run and since we have defined the variable `a` globally in the function, the `print()` function will still print the value `3` on the screen, as it can reach the variable `a`.

Here, we learned how to create a **global** variable in a function with the **global** expression. But here's a point I have to tell you, because **global** variables are accessible everywhere, your code can cause complexity, so using **global** variable and **global** expression is not recommended. It's better and readable to define and use locally for the function.

## You might also like

[Python Functions](https://baransel.dev/post/python-functions/)
[Python File Operations](https://baransel.dev/post/python-file-operations/)
[Python Set and Frozenset](https://baransel.dev/post/python-set-and-frozenset/)
[Python dictionary and dictionary methods](https://baransel.dev/post/python-dictionary-and-dictionary-methods/)
[Python tuple and tuple methods](https://baransel.dev/post/python-tuple-and-tuple-methods/)