Home » Python » Python Variables

What is a Variable in Python?

A variable represents a memory location in which some value of a particular data type is stored. This data type can be integer, string, list, tuple, and so on. A variable is an alternative easy way to store and retrieve data in the memory.

When we store anything in the memory, our operating system allocates the required amount of space and give it a memory address. But these memory addresses are in alphanumeric form and very hard to remember, e.g. 02tt34, 8765th, etc. So, if we want to retrieve the stored data we have to provide these addresses to the system. It would be a very hectic process.

To avoid this problem, a human readable name is given these address spaces so that we can access them very easily. Ex: name of a song, movie, file stored in your PC.

A variable is also a name or an identifier of a memory location that has some data stored in it. So instead of using memory addresses, one can easily access that data by using its variable name. For example: name="Jack", age=45; the variables are name and age with values Jack and 45.

Allocates is a name which is used to refer memory location. In simple it is a reserved memory location to store values, also known as identifier. Variable while declaring it. The interpreter implicitly binds the value with its type. Python enables us to check the type of the variable used in the program.

Note : If you want to see the data type of a variable then use type function. Ex. print(type(name))

How to declare a variable in Python

Python is a type infer language, i.e., variable's data type need not to be declared. Just store valid value in the variable and Python will automatically detect its data type. It is to be noted that each variable in Python is an object (Read: Data types in Python).

To store data in a python variable just write variable_name=value. Ex. a=10, name="Rohit". You have to remember that variable name is case sensitive. What does it mean? Continue reading below!

Python variable naming rules

Variable names can be very short of only one character(a=10, f=5, _ab="true") and also very long as needed by the developer. But it is always recommended to use meaningful variables names(Student_name, address) rather than single characters.

Variables names are bounded within certain rules. If these rules are violated then it will be an invalid variable, the rules are:

  • A variable name must start with an alphabet or the underscore character.
  • It should never start with a number.
  • No special characters should be used in a variable name. Ex. @, #, $, %, ^, etc.
  • Variables are case sensitive, i.e., uppercase and lowercase alphabets are treated as separate variables. For example: Age, age, AGE, _Age are 4 different variables for Python.

Assign values to variables in Python

In Python, variables are assigned values using assignment operator '='. For example: a=10, Employee_name="Kylie". Have you noticed the use of inverted commas in string type data? It is a rule to enclose a string within single(' ') or double(" ") or triple(''' ''') inverted commas, otherwise it will be an invalid string.

For integer and float data type just enter the values as they are without any inverted commas, and it will be stored in the memory. Check out the example below to see how to assign values for different data types in python.

Note : The methods, echo() and print(), are used to print the values stored in variables.
Example
id=10; Employee_name="Jack Sparrow"; Contact=5673829134; Assessment_marks=93.5; print (id) print (Employee_name) print (Contact) print (Assessment_marks)

Output

10
Jack Sparrow
5673829134
93.5

You can change variable's value by assigning a different value to the same variable. However, when you change values then different data types behave differently. Some override the existing value, others leave the previous memory space and allocate a new memory space for the new value. You can learn more about it in our data type in python tutorial.

Look at the example below to understand how to change variable value in python.

Example
demo_no1=10; demo_no1=20; demo_str1="Hello"; demo_str1="Python!"; print (demo_no1) print (demo_str1)

Output

20
Python!

Assigning multiple values to multiple variables

In python, you can assign multiple variables in a single line. Just write all variable names to the left side of assignment operator and the respective values to its right side. Look at the example below:

Example
a, b, c = 5, 3.2, "Hello" print (a) print (b) print (c)

Output

5
3.2
Hello

If you have more than one variable with same value, then there is no need to assign values separately for each variable. Look at the example below to understand how to assign same value to multiple variables at once:

Example
x = y = z = "Welcome to Python!" print (x) print (y) print (z)

Output


Welcome to Python!
Welcome to Python!
Welcome to Python!