What are Variables and Literals in Python?

Lets try to understand what are variables and literals? one by one and later in context with Python Programming language we will try to establish role of variables and literals in Python..


Variables can be thought of as containers (named memory locations in true sense) which can be used to hold data. For computers it’s all 0s (zeros) and 1s (ones) but for us data exists in different shapes (types) and sizes. When we create a variable in any programming language we are reserving a memory area by some name (variable name) to hold or store some value. That value might be of different type – numbers – integers (both negative and positive values but only whole numbers), floats (fractions i.e whole number along with fraction part separated by decimal point), Strings (Sequence of Characters) and so many other types depending upon the language.

What are Python Variables?

Look at the example below:

print(FName, LName)

Variables in Python – In the example above FName and LName are variables storing the values “Pawan” and “Arora” respectively.

Python is a Dynamic typed Language. Hence Variables in Python are references instead of place holders. In Python:

  • Values (or data) is not assigned to variables.
  • Data is stored at some memory address and the address of that location is assigned to the variable.
  • So if you assign some value to a variable, Python actually checks if that value is already there at some memory location or not. If exists, instead of allocating new memory block it simply stores the reference (address) of that location to a variable name.
  • So every time you manipulate value of a variable – Python simply assigns a new memory address to all the Python variables after storing that modified value to new location.

Literals (Constants)

Literals or constants are the values that are used as it isĀ  (without storing them in some container i.e. without using any variable). These values cannot be altered (changed). For example 83, 56.34 or “Happy Learning” all of them are literals.

What are Python Literals?

In Python you can used literals as it is and no program instruction can modify that constant value. While programming we use combination of ariables and constants – variables to hold data which can be manipulated, to hold intermediate results of the calculations and constants to represent fixed values, for example the value of PI which is a fixed value 3.14.

Example below shows the use of Literals and variables.

Age=27 #Age is a variable and 27 is literal of type integer
URL="edukers.in"  # URL is a variable and "www.edukers.in" is a literal of type string
amount=345.34  #aomount is a variable and 345.34 is a literal of float type

Variable Naming Rules in Python

In Python while naming variables, follow the following rules:

  • Variable names may contain letters, digits (0-9) or the underscore character _.
  • Variable names must begin with a letter from A-Z or the underscore _ character. Either lowercase or uppercase letters are acceptable.
  • Variable names may not be a reserved word in Python.