Hellow Programmers, In this python tutorial I am gonna talk about variables & their types. So What is Variable?

Variable: Think about an empty pot in which you can put anything. In programming we call it variable. Actually a variable is an empty memory location to store values.

How to declare a variable in python?

Declaring a variable in python is a piece of cake. Name it & put a value into it. As python is a dynamically typed language we don’t need to declare variable types. Python is smart enough to detect the type itself.

There are some rules of declaring variables:

  • A variable name must start with a letter or the underscore character.
  • Cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores. For example, anything like this is valid: A-z, 0–9, and _
  • Variable names are case-sensitive (height, Height, and HEIGHT are three different variables names)
name = value
a = 10 #integer type
b = 1.1 #float type
c = 'Hello World' #string

We can also store list, tuple, dictionary, set & other data types in variables.

a_list = [1,2,3,4]

a_tuple = ('a','b','c')

a_dictionary = {
    1:'One',
    2:'Two',
    3:'Three',
}

The amazing thing about a python variable is that you can alter the value at any time. As we have created a variable ‘a’ = 10, we can put different value into it.

a = 10
print(a)
a = "Value Changed"
print(a)

Output

10
Value Changed

Python Variable Types: Local Variable  & Global Variable

Python has three types of variables:

  • Global variable
  • Local variable
  • Nonlocal Variable

Global Variable: is a variable which is stored in global namespace and can be accessed from anywhere of the program. If you want to use a  variable to be the same for the whole program or module we declare it as a global variable.

Local Variable:  is a variable which is stored in local namespace(inside classes or functions) and can be accessed only from that class or function. if you want to use the variable in a specific function or method, you use a local variable while Python variable declaration.

x = "I am global"

def func():
    x = 'I am local'
    print("x:", x)

func ()
print("x:", x)

Output:

x: I am local
x: I am global

How to make you a local variable global?

If we want to change a global variable from local scope we use ‘global’ keyword. We use ‘global’ keyword to read and write a global variable only if it is inside a function otherwise it has no effect.

a = 10
b = 20

def add():
    a = a + 5
    sum = a + b
    print(sum)
   
add()

See What Python is Saying:

Traceback (most recent call last):
File "<string>", line 12, in <module>
File "<string>", line 7, in add
UnboundLocalError: local variable 'a' referenced before assignment

It is saying that local variable ‘a’ is been referenced before assignment. This is happening because we are trying to modify the global variable from local scope but python doesn’t allow us to do it. We are only allowed to access the global variable form local scope. Here the ‘global’ variable comes to save us from this hassle.

a = 10
b = 20

def add():
    global a 
    a = a + 5
    sum = a + b
    print(sum)
    
add()

#OUTPUT
35

Now, If you check the value of ‘a’ from global scope you will see that the value is changed from 10 to 15.

Non Local Variable:

Python has another type of variable. ‘Nonlocal Variable. It is used in nested functions(a function inside another function). This variable is neither in the global scope nor in the local scope.

we use “nonlocal” keyword to create nonlocal variables.

def outer():
    x = "local"
  
    def inner():
        nonlocal x
        x = "nonlocal"
        print("x(from inner):",x)
        
    print("x(before calling inner)=",x)
    inner()
    print("x(from outer):", x)


outer()

Output:

x(before calling inner)= local
x(from inner): nonlocal
x(from outer): nonlocal

Constants

A constant is also a variable but we can not change it’s value. In python constants are not unchangeable but should not changed. We write the name of the variable in UPPERCASE to indicate it as a constant. It is just a convention it can’t resist from reassignment.

PI = 3.1416
THE_CREATOR_OF_PYTHON = "Guido Van Rossum"

+ posts

Author | Python-Django Developer

+ posts

Full-stack Developer (Python | Django | React | React-Native | Angular | Vue)