Python Data Types

Data type refers to the type of data that a variable is storing. For example –

if a = 1, The data type of the variable ‘a’ is int(Since it is storing an integer value).

In python, we do not need to specify the data type of a variable at the time of variable declaration (Unlike other programming languages). The python interpreter automatically detects the data type of a variable on the basis of the type of the value.

For example, in C++ we strictly need to specify the data type of a variable at the time of declaration.

int x;
x = 1;

In the above example C++ code, we have declared a variable named ‘x’.

If you want to check the data type of a variable, you can do it with the help of a predefined function named ‘type()’. The type() function simply return the type of the value a variable is holding.

Consider the following example-

x = "Hello"
y = 7
z = 1.5 
print(type(x))
print(type(y))
print(type(z))

Output :

<class 'str'>

<class 'int'>

<class 'float'>

Standard data type in Python

A variable can hold different types of values one at a time. For example string value, integer value or a decimal point value(called as float). If you want to store the name of a person or may be his/her email id, you would store it as a string, but if you want to store the age, you would store it as an integer value.

Python provides the following standard data types –

  • Numbers
  • Sequence types
  • Boolean
  • Dictionary
  • Set

Numbers

This data type stores numeric value. Integer, float and complex number belongs to this.

  • Int – Integer numbers belongs to this category. They can be of any length such as 1,56,1124, -170 etc.
  • Float – Float is used to store numbers with decimal values such as 1.5, 11.4, 5.7, 12.67 etc.
  • Complex – A complex number is a number that has two parts. One is real part and the other one is imaginary. For example 7 + 4j in which 7 is the part and 4 is the imaginary part. Some examples of complex numbers are 4+5j, 2+ 6j etc.

Python creates objects of numbers when a numeric value is assigned to a variable. For example-

x = 4
print("The type of x is ", type(x)) 
y = 1.4
print("The type of y is ", type(y))
z = 4 + 7j
print("The type of z is ", type(z))

Output :

The type of x is <class 'int'>

The type of y is <class 'float'>

The type of z is <class 'complex'>

Sequence type

The sequence type contains strings, lists and tuples

String-

A string is a series or group of characters enclosed in quotes. In python we can use single, double and triple quotes to define a string. However the three types of quotes are used specifically in different cases. (We will learn more about strings in a separe lesson).

username = "aman"
email = 'aman@gmail.com'
address = '''4/52, sector 21, kharghar, Navi Mumbai'''

Lists-

Lists are identical to arrays in C/C++. However unlike arrays, they can contain values of different types. Items in a list are enclosed inside square brackets and are separated by commas. Lists are ordered and mutable (can be modified after creation).

Consider the below given example-

mylist = ["orange", 24, "blue", 1.2]
print(mylist)

Output :

["orange", 24, "blue", 1.2]

When a list is created default keys of the elements are generated starting from 0. For example the key of the first element is 0, key of second element is 1 and so on. An element of a list can be fetched by its key.

For example-

mylist = ["orange", 24, "blue", 1.2]
print(mylist[1])

Output :

24

Since the key of the element 24 is 1, the expression mylist[1] returned 24.

Tuple

Tuple is similar to a list, like we can store a collection of different types of values. Items in a tuple are enclosed in round brackets () and are separated by commas. However, unlike list a tuple is only a read only entity. It means once you’ve created a tuple, you cannot modify it. A tuple is ordered and immutable. Like lists, default indexes or keys are generated starting from 0 at the time of initializing a tuple.

Consider the below example –

mytuple = ("car", 2457, 22.4, "rose")
#printing the tuple
print(mytuple)
#checking the type of the variable mytuple
print(type(mytuple))

Output :

("car", 2457, 22.4, "rose")

<class 'tuple'>

Dictionary

If you have read about associative arrays in some other programming language, you would easily understand what a dictionary is. A dictionary is a collection of items in key value pairs. Each key refers to a distinct value. A dictionary is unordered and mutable.

The items in a dictionary are enclosed within curly braces {} and are separated by commas. Any item of a dictionary can be fetched by its respective key.

Consider the below example-

mydict = {"name":"John", "age":24}
print(mydict["name"])

Output :

John

Boolean

The boolean has two values – true and false. They are used to find whether a given statement is true or false.

Consider the below example-

x = true
print(type(x))

Output :

<class 'bool'>

Note: If you print a condition, you would get a boolean value.

For example-

x = 4
print(x < 10)

Output :

true

Set

Set is an unordered collection of data. It has unique elements and is mutable. In set, the order of the elements is not static. With every execution of the program, it may return changed sequence of the elements.

Consider the below example-

myset = {"red", 25}
print(myset)

Output :

{"red", 25}