Variables in python

Variables are a fundamental concept in programming. Variables are used to store data. Variables are stored in a reserved memory location.
Variables in python are case sensitive. That means it is sensitive to lowercase and uppercase characters. That means variables like user and USER are different. Variables can contain different data values like strings, numbers (integer, float), boolean (True or False). Variables can also contain a list, tuple, and dictionary.

You will learn following topics of variables:

  1. Rules for declaring variables
  2. Declaring variables
  3. Redeclaring variable
  4. Concatenation of variables
  5. Deleting a variable
  6. Use of local and global variables:

Rules for declaring variables:

There are some rules to declare a variable. They are as follows:

  1. You cannot name a variable starting with a number.
  2. You must start a variable name with a letter or the underscore character.
  3. You can only use alpha-numeric characters and underscores for naming a variable.

Declaring variables:

See the following example to learn how to declare variables.


str = "Hello World"


Hello World

Redeclaring variable:

You can assign different value to a variable even after it is declared.


str = "Hello World"
str = "Welcome to python tutorial."


Welcome to python tutorial.

Variables storing string, interger and boolean:


name = 'John Doe'
age = 25
height = 5.10
is_Male = True

print('User name is: ', name)
print('Age is: ', age)
print('Height is: ', height)
print('Is User genger Male ? ', is_Male)


In above example, variable values are also printed to see them in terminal.

User name is: John Doe
Age is: 25
Height is: 5.1
Is User genger Male ? True

Variables storing list:

list are used to store collection of values.


names = ['John', 'Jane', 'Jim', 'Jack']


[‘John’, ‘Jane’, ‘Jim’, ‘Jack’]

Variables storing tuple:

tuples are used to store collection of values. But values stored in tuple cannot be modified.


names = ('John', 'Jane', 'Jim', 'Jack')


(‘John’, ‘Jane’, ‘Jim’, ‘Jack’)

Variables storing dictionary:

Dictionaries are used to store information in formats of key value pairs. Dictionaries are defined using {}. keys in dictionaries must have to be unique. Values in dictionaries can be anything like strings, numbers, boolean, etc.


user = {
    "name": "John Doe",
    "age": 40,
    "email": ''


{‘name’: ‘John Doe’, ‘age’: 40, ’email’: ‘’}

Concatenation of variables:

You can join multiple variables using concatenation.


str1 = "Hello"
str2 = "World"
print("Concatenated string is: ", str1 + str2)



Deleting a variable:

You can delete a variable by using del command.


del variable_name


username = "John Doe"
del username


In the above example. we have created a variable named as username and we have deleted it using del command. But when we print variable after deleting it, we get the following error.

NameError: name ‘username’ is not defined

Use of local and global variables:

Global variable:

When you want to use certain variable for rest of your program you have to use it by declaring a global variable.

Local variable:

When you want to use the variable in a specific function or method, you have to use by declaring a local variable.


In the following example, we have declared total as global variable as we may need in another function than addition(). So total variable is accessible throughout the program. In function addition(), we have declared num1 and num2 as local variables. They can be only accessed in addition() function.

def addition():
    num1 = 2
    num2 = 5
    total = num1 + num2



When we print variable from function we get value 7 but we print it outside of function we get value 0. This is because of local and local and global variables. In global variable total we have set its value to 0.



You have learned the following key points from variables in python article. They are as following:

  1. Variables are used to store data types like string, number, boolean, list, tuple, and dictionary.
  2. There are some rules given above which you must follow while declaring variables.
  3. You can assign a different value to a variable even after it is declared.
  4. You can delete a variable using the del command.

See Also:

Strings in python

loops in python

Leave a Reply