Best 7 Data Types of Python: A Complete Guide

Python data types are the building blocks of any Python program. Learn about the top 7 data types of Python and how to use them effectively.

Built-in Data Types in Python

  • Binary Types: memoryview, bytearray, bytes
  • Boolean Type: bool
  • Set Types: frozenset, set
  • Mapping Type: dict
  • Sequence Types: range, tuple, list
  • Numeric Types: complex, float, int
  • Text Type: str

If you are using Python, check data type using the syntax type (variable). Get a detailed insight into what are the common built-in data types in Python and associated terms with this blog.  

1. Python Numbers

We can find complex numbers, floating point numbers and integers in the category of Python Numbers. Complex numbers are defined as a complex class, floating point numbers are defined as float and integers are defined as an int in Python. There is one more type of datatype in this category, and that is long. It is used to hold longer integers. One will find this datatype only in Python 2.x which was later removed in Python 3.x. 

“Type()” function is used to know the class of a value or variable. To check the value for a particular class, “isinstance()” function is used. 

  • Integers:
    • There is no maximum limit on the value of an integer. The integer can be of any length without any limitation which can go up to the maximum available memory of the system. 
  • Integers can look like this:
    • >>> print(123123123123123123123123123123123123123123123123123 + 1)

123123123123123123123123123123123123123123123123124

  • Floating Point Number:
    • The difference between floating points and integers is decimal points. Floating point number can be represented as “1.0”, and integer can be represented as “1”. It is accurate up to 15 decimal places.
  • Complex Number:
    • “x + yj” is the written form of the complex number. Here y is the imaginary part and x is the real part.

2. Python List

An ordered sequence of items is called List. It is a very flexible data type in Python. There is no need for the value in the list to be of the same data type. The List is the data type that is highly used data type in Python. List datatype is the most exclusive datatype in Python for containing versatile data. It can easily hold different types of data in Python.  

Lists are among the most common built-in data types in Python. Like arrays, they are also collections of data arranged in order. The flexibility associated with this type of data is remarkable. 

It is effortless to declare a list. The list is enclosed with brackets and commas are used to separate the items. 

A list can look like this:

>>> a = [5,9.9,’list’]

One can also alter the value of an element in the list.

Complexities in declaring lists:

Space complexity: O(n)

Time complexity: O(1)

How to Access Elements in a Python List

 

Programmers refer to the index number and use the index operator [ ] to access the list items. In Python, negative sequence indexes represent the positions placed at the end of the array. 

Therefore, negative indexing means starting from the items at the end, where -1 means the last item, -2 means the second last item, and so on. 

How to Add Elements to a Python List

 

There are three methods of adding elements to a Python list:

Method 1: Adding an element using the append() method 

Using the append() method, you can add elements in this Python data type. This is ideally suited when adding only one element at a time. Loops are used to add multiple elements using this method. Both the time and space complexity for adding elements in a list using the append() method is O(1). 

Method 2: Adding an element using the insert() method 

Unlike the append() method, the insert() method takes two arguments: the position and the value. In this case, the time complexity is O(n), and space complexity is O(1). 

Method 3: Adding an element using extend() method

Alongside the append() and insert() methods, there is one more method used to add elements to a Python list known as the extend() method. The extend() method helps add multiple elements at the end of the list simultaneously. Here, the time complexity is O(n), and the space complexity is O(1). 

How to Remove Elements from a Python List

Removing elements from a Python list can be done using two methods:

Method 1: Removing elements using the remove() method

This built-in function can be used to remove elements from a Python list. Only one element can be removed at a time using this function. 

If the element whose removal has been requested does not exist in the list, an error message pops up. Removing elements using the remove() method takes a time complexity of O(n) and a space complexity of O(1). 

Method 2: Removing elements using pop() method 

The pop() function can also help eliminate and return an element from this Python data type. However, by default, the function only removes the last element of the list. 

If you want to remove any element from any specific position, provide the index of the element to be removed in the argument of the pop() function. 

In this functionality, the time complexity for removing the last element is O(1)/O(n) O(1), and that for removing the first and middle elements is O(n). The space complexity in this case is O(1). 

3. Python Tuple

A Tuple is a sequence of items that are in order, and it is not possible to modify the Tuples. The main difference list and tuples are that tuple is immutable, which means it cannot be altered. Tuples are generally faster than the list data type in Python because it cannot be changed or modified like list datatype. The primary use of Tuples is to write-protect data. Tuples can be represented by using parentheses (), and commas are used to separate the items. 

Tuples can look like this:

>>> t = (6,’tuple’,4+2r)

In the case of a tuple, one can use the slicing operator to extract the item, but it will not allow changing the value.

4. Python Strings

A String is a sequence of Unicode characters. In Python, String is called str. Strings are represented by using Double quotes or single quotes. If the strings are multiple, then it can be denoted by the use of triple quotes “”” or ”’. All the characters between the quotes are items of the string.

One can put as many as the character they want with the only limitation being the memory resources of the machine system. Deletion or Updation of a string is not allowed in python programming language because it will cause an error. Thus, the modification of strings is not supported in the python programming language.

A string can look like this:

>>> s = “Python String”

>>> s = ”’a multi-string

Strings are also immutable like tuples and items can be extracted using slicing operators [].

If one wants to represent something in the string using quotes, then they will need to use other types of quotes to define the string in the beginning and the ending.

Such as: 

>>> print(“This string contains a single quote (‘) character.”)

This string contains a single quote (‘) character.

>>> print(‘This string contains a double quote (“) character.’)

This string contains a double quote (“) character.

5. Python Set

The Collection of Unique items that are not in order is called Set. Braces {} are used to defined set and a comma is used to separate values. One will find that the items are unordered in a set data type.

Duplicates are eliminated in a set and set only keeps unique values. Operations like intersection and union can be performed on two sets. 

Python set will look like this:

>>> a = {4,5,5,6,6,6}

>>> a 

{4, 5, 6}

The slicing operator does not work on set because the set is not a collection of ordered items, and that is why there is no meaning to the indexing of set.

6. Python Dictionary

Dictionary is a type of python data type in which collections are unordered, and values are in pairs called key-value pairs. This type of data type is useful when there is a high volume of data. One of the best functions of Dictionaries data type is retrieving the data for which it is optimized. The value can only be retrieved if one knows the key to retrieve it. 

Braces {} (curly brackets) are used to define dictionaries data type in Python. A Pair in the dictionary data type is an item which is represented as key:value. The value and the key can be of any data type.

Python Dictionary can look like this:

>>> d = {3:’key’,4:’value’}

7. Boolean Type

There can be only two types of value in the Boolean data type of Python, and that is True or False. 

It can look like this:

>>> type(True)

<class ‘bool’>

>>> type(False)

<class ‘bool’>

The true value in the Boolean context is called “truthy”, and for false value in the Boolean context, it is called “falsy”. Truthy is defined by the objects in boolean, which is equal to True, and in the same way, Falsy is defined by the objects equal to falsy. One can also evaluate Non-Boolean objects in a Boolean context.

#python 

Best 7 Data Types of Python: A Complete Guide
2.30 GEEK