Data types and Variables in Python

Data types in Python are (as in most programming languages) something that determines what kind of data a variable can contain (e.g, integers, decimal numbers, booleans, or references) where the data type is determined in the declaration of the variable. It is important to declare the correct data type so that you do not accidentally mix different data types with each other. Then there is the risk that the program will not work properly or even crash.

VARIABLES IN PYTHON

Variables in Python are used to store data / values in the computers memory. In programming, large amounts of data are often handled. It is therefore important to be able to efficiently separate and structure the data. A variable has a name and value of a particular data type.

Variables in Python are used to store data / values in the computers memory.

HOW DO YOU ASSIGN VALUE TO VARIABLES IN PYTHON ?

Let’s take an example to show how you can assign a value to a variable. We easily create a variable in Python by typing:

 

varA = 10

In this case, the variable has the name varA, has been assigned the value 10, and is the data type integer

DATA TYPES IN PYTHON

Different type of data contains different kinds of information. For example, a number is not the same as a text string. A data type is the category that the variable contains. Examples of data types are:

 

  • Int: integer, for example, 1, 2, 3, 4.
  • String: A text string, such as “Programming”, or “Python”.
  • Float: a floating point, for example 10.2, 23.5 or 2.0.
  • Boolean: Boolean value. Contains True or False. True and False can also be represented by 1 and 0 respectively.

HOW DO YOU DECLARE VARIABLES OF A CERTAIN DATA TYPE IN PYTHON?

In Python you declare (assign) values to variables with the “=” operator. Let’s see some examples of variables we can create:

 

# Integer 
a = 10 

# Text string 
b = "Programming in Python"  

# Float 
c = 10.0 

# Boolean 
d = True
Data types in Python

In Python you do not have to decide what kind of data type the value is, it is actually done automatically. But you have to be careful how you declare the variables. For example, check what happens if you declare the following variables:

 

a = True 

b = "True"

In this case, variable a is data type boolean, while b is a string. So there are two completely different values and this is very important to be aware of when programming. For example, the following code causes the program to crash:

 

a = 10 

b = "4" 

print(a + b)

This will cause an error and the program will fail. The variable b in this case contains a text string and not an integer that you might think. In Python, you cannot add a text string to an int. Therefore, be careful what you store in your variables so that you do not accidentally cause errors to your program.

HOW DO YOU KNOW THE DATA TYPE OF A VARIABLE ?

If you do not know the data type a variable contains, you can simply use the type () function that returns the data type that a variable has. In the following example:

 

a = 10
b = "Programming in Python"

print("The variable a is data type: ", type(a))
print("The variable b is data type: ", type(b))

 

Returns the print:

 

The variable a is data type: <class ‘int’>
The variable b is data type: <class ‘str’>

That is, variable a is of type int (integer) and variable b is of type string (text string)