Commonly Used Data Types in Python

Tusamma Sal Sabil
3 min readMar 14, 2020

--

Data types in Python

Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

There are various data types in Python. Some of the important types are listed below.

Python Numbers

Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as int, float and complex class in Python.

We can use the type() function to know which class a variable.

# Here a is int
a = 5
print(a, "is of type", type(a))# Here a is float
a = 2.0
print(a, "is of type", type(a))# Here a is complex
a = 1+2j
print(a, "is of type", type(a))

Python List

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.

Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

# Here a is list
a = [1, 2.2, 'python']
print(a, "is of type", type(a))

Lists are mutable, meaning, value of elements of a list can be altered.

# List is mutable
a = [1, 2.2, 'python']
a[2] = 'list is mutable'print(a)

Terminal Output: [1, 2.2, ‘list is mutable’]

Python Tuple

Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified.

Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.

It is defined within parentheses () where items are separated by commas.

# Here a is tuple
a = (1, 2.2, 'python')
print(a, "is of type", type(a))

Tuples are immutable, meaning, value of elements of a tuple cann’t be altered.

# Tuple is immutable
a = (1, 2.2, 'python')
a[2] = 'tuple is immutable'print(a)

Terminal Output: TypeError: ‘tuple’ object does not support item assignment

Python Strings

String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ‘’’ or “””.

# Here a is single string
a = "This is a string"
print(a, "is of type", type(a))# Here a is multiline string
a = '''This is a multiline string
Line 1
Line 2'''
print(a, "is of type", type(a))

Terminal Output:

This is a string is of type <class ‘str’>

This is a multiline string

Line 1

Line 2 is of type <class ‘str’>

Python Set

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.

# Here a is set
a = {1,2,2,3,3,3}
print(a, "is of type", type(a))

Terminal Output: {1, 2, 3} is of type <class ‘set’>

Python Dictionary

Dictionary is an unordered collection of key-value pairs.

It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.

# Here a is dict
a = {'key1':'value1','key2':'value2'}
print(a, "is of type", type(a))

Terminal Output: {‘key1’: ‘value1’, ‘key2’: ‘value2’} is of type <class ‘dict’>

Python Frozenset

The frozenset() method returns an immutable frozenset object initialized with elements from the given iterable.

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of frozen set remains the same after creation.

Due to this, frozen sets can be used as key in Dictonay or as element of another set. But like sets, it is not ordered (the elements can be set at any index).

The example of frozenset() method is:

person = {"name": "John", "age": 23, "sex": "male"}# Here a is frozenset of person dict
a = frozenset(person)
print(a, "is of type", type(a))

Terminal Output: frozenset({‘age’, ‘sex’, ‘name’}) is of type <class ‘frozenset’>

Python Boolean

The Boolean (True or False) using the standard truth testing procedure.

# Here a is boolean
a = True
print(a, "is of type", type(a))

Terminal Output: True is of type <class ‘bool’>

GitHub Link: Python Data Types

--

--