Python Data Types
Python is a dynamically typed language; we don't need to define the types for variables during the declaration because the interpreter implicitly binds the value with its type.
Every value in Python has a datatype. Since everything in Python is an object, data types are classes, and variables are instances of these classes.
Standard Data Types
In Python, a variable can hold different types of values. For example, a person's age must be stored as an integer, whereas its name must be stored as a string.
Python offers different data types. The major data types in Python are given below:

Getting the Data Type
In Python, to get the data type of any object (or instance), you can use the type()
function.
In the following example, we will assign two different values and output their types.
a = "Hello World!"
b = 12
print("Type of a : ", type(a))
print("Type of b : ", type(b))
After executing the above code, the output will be as follows:
Type of a : <class 'str'>
Type of b : <class 'int'>
Python Numbers
The Python numbers category include integers number, floating point numbers, and complex number. They are defined as int
, float
, and complex
classes in Python.
In the following example, we use the type()
function to know which class a variable or a value belongs to. You can also use the instance()
function to check if an object belongs to a particular class.
a = 9
print(a, "is of type", type(a))
b = 4.0
print(b, "is of type", type(b))
c = 6+4j
print(c, "is of type", type(c))
print(a, "is int number?", isinstance(a, int))
After executing the above code, the output will be as follows:
9 is of type <class 'int'>
4.0 is of type <class 'float'>
(6+4j) is of type <class 'complex'>
9 is int number? True
Python offers three types of numeric data:
Int
- Integer value can be any length. It is only limited by the memory available.Float
- A floating-point number is accurate up to 15 decimal places. Decimal points separate integer and floating points. For example,3
is an integer,3.0
is a floating-point number.Complex
- complex numbers are written in the form,x + yj
, wherex
is the real part andy
is the imaginary part.
In the following example, we can see the three types of numeric data:
x = 111122223333444455556666777788889999
print("x : ", x)
y = 0.111122223333444455556666777788889999
print("y : ", y)
z = 4 + 8j
print("z : ", z)
After executing the above code, the output will be as follows:
x : 111122223333444455556666777788889999
y : 0.11112222333344446
z : (4+8j)
As you can see, the float
variable y
got truncated because the floating-point number is accurate up to 15 decimal places.
Python Strings
The String data type is a sequence of characters. You can use single quotes 'hello'
or double quotes "hello"
. You can also use multi-line strings by using triple quotes '''
or """
.
a = "Hello World!"
print(a, "is of type", type(a))
b = '''This is a multiline
string'''
print(b, "is of type", type(a))
The output of the above code is as follows:
Hello World! is of type <class 'str'>
This is a multiline
string is of type <class 'str'>
The operator +
is used to concatenate two strings, so the operation "hello" + "world"
returns "hello world"
.
The operator *
is the repetition operator, so the operation "hello " * 2
returns "hello hello"
You can also use the slicing operator [:]
to extract characters or a range of characters, but you can't change them because the string is immutable.
print("hello " + "python " + "world")
print("hello " * 3)
a = "Hello World!"
print("a[8] : ", a[8])
print("a[8:12] : ", a[8:12])
# It generates an error. In Python, Strings are immutable in Python
a[3] = 'p'
The output of the above code is as follows:
hello python world
hello hello hello
a[8] : r
a[0:5] : Hello
TypeError Traceback (most recent call last)
----> 7 a[3] = 'p'
TypeError: 'str' object does not support item assignment
Python List
The List data type is an ordered sequence of items. It is one of the most used data types in Python. In Python, a list can contain items of different types.
The items stored in a list are separated with a comma ,
and enclosed within square brackets []
.
The concatenation operator +
and repetition operator *
works with the list in the same way as strings.
We can use the slicing operator [:]
to extract an item or a range of items from a list. In Python, the index starts from 0.
Let us consider the following examples:
list1 = [ 5, "hello", 1.0, "world", "python", "hi" ]
# displaying the type of the given list
print(type(list1))
# printing the list1
print(list1)
# list slicing from the 4 index
print(list1[4:])
# list slicing from 0 to 3 index
print(list1[0:3])
# list concatenation using + operator
print(list1 + list1)
# list repetition using * operator
print(list1 * 4)
The output of the above code is as follows:
<class 'list'>
[5, 'hello', 1.0, 'world', 'python', 'hi']
['python', 'hi']
[5, 'hello', 1.0]
[5, 'hello', 1.0, 'world', 'python', 'hi', 5, 'hello', 1.0, 'world', 'python', 'hi']
[5, 'hello', 1.0, 'world', 'python', 'hi', 5, 'hello', 1.0, 'world', 'python', 'hi', 5, 'hello', 1.0, 'world', 'python', 'hi']
Lists are mutable, which means the value of an item of a list can be altered.
list1 = ["a", "b", "c", "d", "e"]
print(list1)
list1[2] = "hello"
print(list1)
The output of the above code:
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'hello', 'd', 'e']
Python Tuple
The Tuple data type is an ordered sequence of items. It is similar to the list in many ways. Like lists, tuples can also contain a collection of items of different data types.
The tuple items are separated with a comma ,
and enclosed in parentheses ()
.
The difference between a tuple and a list is that the tuple is immutable. It means tuples, once created, cannot be modified.
Let us see the following example:
# creating the tuple
tup1 = (5, "hello", "world", 2, "python")
# displaying the type of the given tuple
print(type(tup1))
# tuple slicing
print(tup1[3:])
print(tup1[0:2])
# tuple concatenation using + operator
print(tup1 + tup1)
# tuple repetition using * operator
print(tup1 * 3)
# changing a value of a tup. It will throw an error.
tup1[3] = "hi"
The output of the above code will be as follows:
<class 'tuple'>
(2, 'python')
(5, 'hello')
(5, 'hello', 'world', 2, 'python', 5, 'hello', 'world', 2, 'python')
(5, 'hello', 'world', 2, 'python', 5, 'hello', 'world', 2, 'python', 5, 'hello', 'world', 2, 'python')
TypeError Traceback (most recent call last)
---> 18 tup1[3] = "hi"
TypeError: 'tuple' object does not support item assignment
Python Set
The Set data type is an unordered collection of unique items. It is iterable and mutable (can be modified after the creation).
The order of elements in a Set is undefined.
The Set can be created using the built-in function set()
or by using a sequence of items inside curly braces {}
and separated by commas.
The Set can contain different types of unique values.
Let us see the following example:
# creating an empty set
set1 = set()
# creating a set with items
set2 = {"apple", 6, "orange", 3, "python"}
# displaying the type of the given set
print(type(set2))
# printing the set value
print(set2)
# adding item to the set
set2.add("kiwi")
print(set2)
# removing item from a set
set2.remove("apple")
print(set2)
# a set has unique values. It eliminates duplicates
set3 = {1, 1, 6, 6, 6, 5, 5, 9, 5, 9}
print(set3)
After executing the above code, the output will be as follows:
<class 'set'>
{3, 6, 'orange', 'apple', 'python'}
{3, 6, 'orange', 'apple', 'python', 'kiwi'}
{3, 6, 'orange', 'python', 'kiwi'}
{1, 5, 6, 9}
Since the Set data type is an unordered collection, indexing has no meaning, so the slicing operator []
does not work with a Set.
set0 = {"a", "b", "c", "d"}
set0[2]
TypeError Traceback (most recent call last)
----> 2 set0[2]
TypeError: 'set' object is not subscriptable
Python Dictionary
The Dictionary data type is an unordered collection od key-value pairs.
In a Dictionary, a key can hold any primitive data type, whereas a value can hold an arbitrary Python object.
A Dictionary is generally used when you have a huge amount of data. It is optimized for retrieving data. To retrieve the value, you must know the key.
A Dictionary is defined within curly braces {}
, and each item is pair in the form of key:value
separated by commas.
Let us see the following example:
# creating a dictionary
d = {1: "apple", 2:"orange", 3: "kiwi", 4:"watermelon"}
# displaying the type of the given dictionary
print(type(d))
# printing a dictionary
print(d)
# accessing value using keys
print("1st fruit is " + d[1])
print("3rd fruit is " + d[3])
# printing all keys
print(d.keys())
# printing all values
print(d.values())
The output of the above code is as follows:
<class 'dict'>
{1: 'apple', 2: 'orange', 3: 'kiwi', 4: 'watermelon'}
1st fruit is apple
3rd fruit is kiwi
dict_keys([1, 2, 3, 4])
dict_values(['apple', 'orange', 'kiwi', 'watermelon'])
Python Boolean
Boolean type offers two built-in values, True
and False
. These values are used to determine if a statement is true or false.
In Python, True
can be represented by any non-zero value or T
, whereas False
can be represented by the 0
or F
.
Let us see the following example:
print(type(True))
print(type(False))
a = (True == 1)
b = (False == 0)
print("a is : ", a)
print("b is : ", b)
The output of the above code is as follows:
<class 'bool'>
<class 'bool'>
a is : True
b is : True
Conversion between Data Types
You can convert between different data types using different type conversion functions like int()
, float()
, str()
, etc..
>>> float(9)
9.0
The conversion from float to int will truncate the value.
>>> int(6.36)
6
>>> int(-6.36)
-6
Compatible values must be used when converting to and from a string type.
>>> int("30")
30
>>>str(85)
'85'
>>>float(63sd)
float(63sd)
^
SyntaxError: invalid syntax
You can also have conversion between sequences (conversion between list
, tuple
, set
).
# from a list to a set
>>> set(['a', 'b', 'c'])
{'a', 'b', 'c'}
# from a set to a tuple
>>> tuple({1, 2, 3})
(1,2,3)
# from string to a list
>>> list('python')
[ 'p', 'y', 't', 'h',' 'o, 'n']
If you want to convert a dictionary, each item must be a pair.
>>> ditct([[1,a], [2, b]])
{ 1: a, 2: b}