×

Menu

What are variables and why do we use them? How to declare variables in python?
Assigning a value to a variable Assigning the value of one variable to another Assigning one value to multiple variables Assigning many values to multiple variables
Variable naming conventions Reserved keywords
Variable types Casting Object id
Conclusion

AI_FOR_ALL

Python 101: Variables and Data Types

Published 10/04/218 min read
image

In this article we are going to look at variables in python. We are going to understand variables by answering some basic questions such as, what are variables?, why do we use variables?, How to declare variables?, what are naming conventions for variables?, etc. We are also going to briefly discuss some basic data types such as numbers and strings. We will be understanding every data type in detail in further articles, So let's get started!!!

What are variables and why do we use them?

Variables in python are reserved memory locations. They act as containers to store data. So basically each variable is pointing to a memory location and the value stored in the variable can be accessed by calling the name of the variable.

Suppose you have to write 1000s of lines of code and there is one value which is occuring repeatedly in your code, now suddenly you want to change that value, one thing you can do is go to each line and change that value manually, which is not feasible. Here variables come into picture. You declare a variable once and you store that value in the variable. Now whenever you want that value anywhere in your code you can get it easily by calling the name of the variable, in which that value has been stored. Now suppose you want to change that value, wherever it appears in your code, you can do that easily by changing the value stored in the variable.

How to declare variables in python?

There is no specific command to declare variables in python. Declaring a variable is pretty straightforward and easy. You just write the name of the variable and assign a value to it.

#These are variables in python
 
var1 = 23 #23 is stored in variable var1
var2 = 23.26 #23.26 is stored variable var2
var3 = "python" #python is stored in variable var3

As we discussed earlier the value stored in a variable can be easily accessed by calling the name of that variable so now, let's print the values we stored in our variables. We can easily do that by using the print statement in python. The print statement is a built-in function in python which we will be discussing in detail when we look at built-in functions.

To print the value stored in a variable just pass the name of that variable as a parameter into the print function print(variable_name).

#Variables in python
var1 = 23 #23 is stored in var1
var2 = 23.26 #23.26 is stored var2
var3 = "python" #'python' is stored in var3
 
#Printing the values stored in variables
print(var1)
print(var2)
print(var3)


OUTPUT:

>> 23
>> 23.26
>> python

Variable assignment

Assigning a value to a variable

As we discussed earlier, there is no special command to assign a value to a variable. To declare a variable just write the name of the variable and assign a value to it. If you change the value of an existing variable i.e. if you assign a new value to a pre-existing variable, the old value will be discarded.

#Variables in python
var1 = 23 #23 is stored in var1
 
#Printing the value stored in var1
print(var1)
 
#Assigning a new value to a pre-existing variable var1
var1 = 26 #26 is the new value assigned to var1
 
#Printing the new value stored in var1
print(var1)


OUTPUT:

>> 23
>> 26

Assigning the value of one variable to another

You can assign the value of one variable to another.

#Variables in python
var = 23 #23 is stored in var1
new_var = var #value of var is assigned to new_var
 
#Printing the values stored in variables
print(var)
print(new_var)


OUTPUT:

>> 23
>> 23

Assigning one value to multiple variables

Python allows chained assignment, which makes it possible to assign the same value to multiple variables simultaneously.

#Variables in python
var1 = var2 = var3 = 23 #23 is stored in var1, var2 & var3
 
#Printing the values stored in variables
print(var1)
print(var2)
print(var3)


OUTPUT:

>> 23
>> 23
>> 23

Assigning many values to multiple variables

You can assign many values to multiple variables simultaneously.

#Variables in python
var1, var2, var3 = 23, 23.26, "python"
#23 is stored in var1
#23.26 is stored in var2
#python is stored in var3
 
#Printing the values stored in variables
print(var1)
print(var2)
print(var3)


OUTPUT:

>> 23
>> 23.26
>> python

Variable naming conventions

There are certain rules you need to follow while naming variables in python.

  • Rule-1: You should start variable name with an alphabet or underscore(_) character.
  • Rule-2: A variable name can only contain A-Z, a-z, 0-9 and underscore(_).
  • Rule-3: You cannot start the variable name with a number.
  • Rule-4: You cannot use special characters with the variable name such as such as $,%,#,&,@.-,^ etc.
  • Rule-5: Variable names are case sensitive. For example var and Var are two different variables.
  • Rule-6: Do not use reserved keywords as a variable names.
#Variables in python
#All these variables are different
var = 20
Var = 21
VAR = 22
vAR = 23
vAr = 24
vaR = 25
_var_ = 26
_VAR_ = 27
v_a_r= 28
 
#Printing the values of these variables
print(var, Var, VAR, vAR, vAr, vaR, _var_, _VAR_, v_a_r)


OUTPUT:

>> 20 21 22 23 24 25 26 27 28

NOTE: while naming variables make sure the name of the variable describes the purpose for which the variable is being used for e.g. if you want to store time you can name your variable as current_time

Reserved keywords

There are certain keywords in python which are reserved to perform specific operations. You are not supposed to use these keywords as variable names.

Python reserved keywords
and as assert break class
continue def del elif else
except false finally for from
global if import is in
lambda None nonlocal not or
pass raise return True try
while with yield

Object Referencing and Identity

What exactly happens when we assign a value to a variable in python? The python variable is a reserved memory location i.e. when you declare a variable, that variable is pointing to the memory location, where the assigned value has been stored. So basically a variable is a name given to the memory location where the object is stored and you can get the object by calling the name of that variable.

image

Python is a an object-oriented language so basically every item of data (variables) in Python is an object of a specific type or class. We will be discussing this in detail when we look at object-oriented programming in python.

Variable types

Data types in python are a way or categorizing or classifying data into different types. A data type tells us what kind of operations can be performed on that particular data. There are 7 different data types in python Numbers, strings, Lists, Tuples, Sets, Dictionaries and Booleans. We will discussing all these data types in detail later.

Variables in python can belong to different data types based on the data assigned to them.

#Variables in python
var1 = 23 #integer variable
var2 = 23.26 #float variable
var3 = "python" #string variable
 
#Printing the type of these variables
print(type(var1))
print(type(var2))
print(type(var3))


OUTPUT:

>> <class 'int'>
>> <class 'float'>
>> <class 'str'>

As we can see above the first variable var1 belongs to integer type and var2 belongs float type and both these integer and float types come under the data type number. Similarly the variable var3 belongs to string data type.

Casting

In python there is no need of specifying the data type of an object while declaring a variable. Python dynamically allocates the data type based on the value assigned to the variable.

If you want to specify the data type of a variable, it can be done by casting.

#Variables in python
var1 = int(23) #integer variable, 23 stored in var1
var2 = float(23) #float variable, 23.0 stored in var2
var3 = str(23) #string variable, '23' stored in var3
 
#Printing the type of these variables
print(var1)
print(type(var1))
print(var2)
print(type(var2))
print(var3)
print(type(var3))


OUTPUT:

>> 23
>> <class 'int'>
>> 23.0
>> <class 'float'>
>> 23
>> <class 'str'>

Here we are using built-in functions int() (integer), float() (float) and str() (string) to specify the data type for variables

Object id

We know that variables are containers to store data and they point to a memory location. If two or more variables have the same value then they point to the same memory location. Let's suppose we have three variables var1, var2 and var3, all have the same value 20, then all these variables will be pointing to the same memory location

image

Now if we change the value of var2, then var2 will be pointing to a new memory location.

image

We can verify the above statements by using the id() function. The id() function is a built-in function in python and it gives us a unique id for a variable. This unique id represents the memory location where the variable is pointing. We will revisit the id() function when we will be looking at built-in functions in python.

#Variables in python
var1 = 20 #20 stored in var1
var2 = 20 #20 stored in var2
var3 = var1 #value of var1 stored in var3
 
#Printing the id of these variables
print(var1)
print(id(var1))
print(var2)
print(id(var2))
print(var3)
print(id(var3))
 
#Let's assign a new value to var3 and check it's id again
var3 = 30 #new value of var3 is 30
print(var3)
print(id(var3))


OUTPUT:

>> 20
>> 11066496
>> 20
>> 11066496
>> 20
>> 11066496
>> 30
>> 11066816

Conclusion

In this article, we looked at variables in python. We understood what are variables, how to declare variables and the different methods of assigning a value to a variable. We also understood memory referencing and object identity of variables by using built-in functions in python. We also looked at naming conventions for variables and discussed the python code for every section.

You can download the jupyter notebook for above post here