Python Data Types
Python Data Types

Python Data Types

INTRODUCTION

There are mainly 5 types of basic data types available in Python

  • Numeric: (integer, Float, Complex)
  • Boolean
  • Sequence: (String, List, Tuple)
  • Set
  • Dictionary 

Understanding these data types is crucial as they dictate how data is stored, manipulated, and used within a python program. 👌

Numeric Data Types

Numeric data types in Python are used to represent numbers and include three distinct type

  • Integer (int): This type is used for whole numbers without any fractional component. For example, 542, and 1000 are integers.
  • Float (float): Floats are used to represent numbers with decimal points, such as 3.140.001, or 2.71828.
  • Complex (complex): Python also supports complex numbers, which have a real part and an imaginary part, denoted as a + bj, where a is the real part and b is the imaginary part. For example, 3 + 4j.

Boolean Data Type

  • The Boolean data type represents one of two values: True or False. This type is particularly useful in control flow statements and logical operations where a condition needs to be evaluated. For instance, in conditional statements, a Boolean expression might determine which block of code is executed.

Sequence Data Types

Sequences in Python are ordered collections of items, and there are several types:

  • String (str): Strings are used to represent text data. A string is a sequence of characters enclosed in single quotes ('...'), double quotes ("..."), or triple quotes ('''...''' or """...""").
  • List (list): Lists are ordered collections of items, which can be of different data types, and are mutable, meaning their contents can be modified. Lists are defined using square brackets, e.g., [1, 2, 'apple', 4.5].
  • Tuple (tuple): Tuples are similar to lists but are immutable, meaning once defined, their contents cannot be changed. Tuples are created using parentheses, e.g., (1, 2, 3).

Set Data Type

  • The Set data type in Python represents an unordered collection of unique items. Sets are useful for operations that require distinct elements, such as union, intersection, and difference. Sets are defined using curly braces, e.g., {1, 2, 3}, or by using the set() function.

Dictionary Data Type

  • A Dictionary in Python is an unordered collection of key-value pairs. Dictionaries are highly optimized for retrieving values when the key is known. They are defined using curly braces, where each key is separated from its value by a colon (:), e.g., {'name': 'John', 'age': 30}.
image

To check the data type of variable use the built-in function type()  and isinstance().

  • The type() function returns the data type of the variable
  • The isinstance() function checks whether an object belongs to a particular class.

Note :

isinstance() is a built-in Python function that checks if a specified object is of the specified type. It returns True if the object is of the specified type, otherwise False.

a = {"a": 1, "b": 2, "c": 3}
b = {"a": 1, "b": 2, "c": 3}
c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

type(a) # dict
isinstance(c, dict) # False

In the code provided, isinstance(c, dict) is checking if c is an instance of the dict (dictionary) type. If c is a dictionary, it will return True, otherwise it will return False.

This is a quick summary of the data types in Python, and it will be the guide for this section.

image

String data type

Integer data type

Float data type

Complex data type

List data type

Tuple data type

Dict data type

Set data type

Additional Data Type to Note

Frozenset

Bool data type

Bytes data type

bytearray

Range data type

memoryview

Summary

Here's a table highlighting the main differences between strings, lists, tuples, dictionaries, and sets in terms of their mutability and key features:

Data Type
Mutable
Key Features
String
No
1. Ordered sequence of characters 2. Immutable 3. Supports indexing and slicing 4. Can contain any characters including escape sequences
List
Yes
1. Ordered collection of items 2. Mutable 3. Supports indexing, slicing, and methods like append(), extend(), remove(), etc. 4. Can contain mixed data types
Tuple
No
1. Ordered collection of items 2. Immutable 3. Supports indexing and slicing 4. Can contain mixed data types 5. Often used for fixed collections of items
Dictionary
Yes
1. Unordered collection of key-value pairs 2. Mutable 3. Keys must be unique and hashable 4. Supports methods like get(), keys(), values(), items(), etc.
Set
Yes
1. Unordered collection of unique items 2. Mutable 3. Does not support indexing or slicing 4. Supports methods like add(), remove(), union(), intersection(), etc.

Explanation

  1. String:
    • Mutable: No
    • Features: Strings are immutable sequences of characters. Once created, the characters within a string cannot be changed. Strings support operations like concatenation, slicing, and various string-specific methods (e.g., upper()split()).
  2. List:
    • Mutable: Yes
    • Features: Lists are mutable sequences, allowing elements to be changed, added, or removed. Lists can contain elements of different types and support a wide range of operations and methods for manipulation.
  3. Tuple:
    • Mutable: No
    • Features: Tuples are immutable sequences, meaning once they are created, their elements cannot be changed. Tuples are often used to represent fixed collections of items and support indexing and slicing.
  4. Dictionary:
    • Mutable: Yes
    • Features: Dictionaries are mutable mappings of unique keys to values. They are unordered (as of Python 3.7+, they maintain insertion order) and provide efficient lookup, insertion, and deletion of key-value pairs. Keys must be hashable and unique.
  5. Set:
    • Mutable: Yes
    • Features: Sets are mutable collections of unique elements. They are unordered and do not support indexing or slicing. Sets provide efficient operations for membership testing, union, intersection, and difference.