Python Data Types

Python is an object-oriented programming language. Every variable in Python is an instance of some class. There are many pre-defined data types in Python. We can create our own classes to define custom data types.


What are the Popular Data Types in Python?

Some of the popular data types in Python are:

  • Numbers – int, float, complex
  • Sequences – String, List, Tuple, Set
  • Map – Dict

How to Identify the Data Type of a Variable?

We can use type() function to find out the data type of a variable.

i = 10

print(type(i))
Python Data Types
Python Data Types

Let’s look at some examples of popular data types in Python.


Python String

Python strings are the instances of class ‘str‘. The string is a sequence of Unicode characters. Python strings are immutable. We can define a string using single quotes (‘) or double quotes(“).

String is the most popular data type in Python. There are various operations supported for string objects – length, format, split, join, slicing etc.

s = "abc"
print(type(s))

s1 = 'hello'
print(type(s1))

print(s[1:2])
print(len(s))
print(s.split('b'))

Output:

<class 'str'>
<class 'str'>
b
3
['a', 'c']

Python Numbers

There are three data types in numbers category – int, float, and complex. There was another data type ‘long’ in Python 2 but it got deprecated in Python 3.

i = 10
print(type(i))

i = 1.23
print(type(i))

i = 1 + 2j
print(type(i))
Python Numbers Data Types
Python Numbers Data Types

Python Tuple

Tuple in Python is an ordered sequence of items. Tuple is immutable i.e. once defined we can’t change its values.

We can define a tuple using parentheses where items are separated using comma. A tuple can contain any number of elements and the elements can be of any type.

t = (1, 2, 3)
print(type(t))

t = ('a', 1, [1, 2])
print(type(t))

Output:

<class 'tuple'>
<class 'tuple'>

Python List

List is almost same as Tuple, except that it’s mutable. The order of the elements is maintained.

List is defined using brackets and the elements are separated using comma.

my_list = [1, 2, 3]
print(type(my_list))

my_list = ['a', 1, [1, 2]]
print(type(my_list))

Output:

<class 'list'>
<class 'list'>

Python Set

Python Set is an unordered collection of items. Set can’t have duplicate values. The order of elements are not maintained in the Set.

A set is defined using braces where the elements are separated using comma. Python Set uses hashing to store elements. So the elements must be hashable i.e. hash() function should work on them. Since List is unhashable, we can’t store a List object in the Set.

my_set = {1, 2, 3, 1}
print(type(my_set))
print(my_set)

my_set = {'a', 1, 'a'}
print(type(my_set))
print(my_set)

Output:

<class 'set'>
{1, 2, 3}
<class 'set'>
{1, 'a'}

Let’s see what happens when we try to have a List as the Set element.

>>> my_set = {1, [1]}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>

Python Dictionary

Python Dictionary is an unordered collection of key-value pairs. It’s defined using braces and the elements are separated using comma. The key and value can be of any type. The key-value pair is defined using colon (key:value).

Python Dictionary objects are of type ‘dict’. They are good to store a large number of values where fast retrieving is required.

my_dict = {"1": "Apple", "2": "Banana"}
print(type(my_dict))

my_dict = {1: 1, 2: 'b', 3: [1, 2]}
print(type(my_dict))

Python dictionary uses hashing on the key to store and retrieve elements, so the key object must support hash() function. That’s why we can’t use a list as the key of dictionary element.

>>> my_dict = {[1]: "a"}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> 

Defining Custom Data Type in Python

We can define a custom data type by creating a class in Python.

class Data:
    pass


d = Data()

print(type(d))
Python Custom Data Type
Python Custom Data Type

Does Python Functions have a Data Type?

So far we have seen that the data type is associated with Python variables. But, does python functions also have a data type?

Let’s check this with a simple program.

def foo():
    pass


print(type(foo))

Output:

<class 'function'>

So Python functions are also have data type. They are the instances of class function.


Does Python Class Methods have a Data Type?

Let’s see if the python class methods also have a data type or not.

class Data:

    def foo(self):
        pass


d = Data()
print(type(d.foo))

Output:

<class 'method'>

So Python class methods have the data type as ‘method’. They are the instances of class ‘method’.


Conclusion

Python is an object-oriented programming language. Everything in Python is an object of some class, even the functions and class methods. We can use built-in type() function to determine the data type of an entity in Python.