# Global and Local Variables in Python with examples

In this tutorial, we are going to learn about the global and local variables in Python with a lot of examples.

Types of the variable in Python is given below:

1. GLOBAL
2. LOCAL

### Global Variable in Python

If the variable is defined outside or inside of any function, and its value can be achieved by any function, that means its scope is entire the program is called Global Variable.

Example: Creating a Global Variable in Python

```a = 10

print(a)```

We created a global variable “a”.

Output:

```10
```

Example: 2

Q: Create a Global variable inside a function.

```a = 2
def define():
global b;
b = 4
c = a + b;
print(c)
define()

Output:

`6`

### Local Variable in Python

If the value of the variable can be achieved by a program in which it is defined, that means its scope is limited to a program, called a local variable.

Example: Creating a Local Variable in Python

```def calculation():
a = 10
a = a*10
print(a)
calculation()
```

we defined a variable inside the function, so “a” is local variable here.

Output:

`100`

Example: Can Local Variable access as Global scope?

```def calculation():
a = 10
a = a*10
print(a)
calculation()
print(a)```

No, as here we are trying to print “a” outside of the function, it gives the output as “a” is not defined.

Output:

```name 'a' is not defined
```

Example: Use a global variable and local variable in same programme.

```a ="Apple"

def fruit_name():
a = "banana"
print(a)

fruit_name()
print(a)
```

We defined “a” as a global first and we are also using “a” as local variable but inside a function name as “fruit_name”.

Output:

```banana
Apple```

In this tutorial, we learned about the global and local variables in Python, and how to use it as different scope or from the given frame of reference.

If you have any doubt, please comment below.