Python Data Types

In programming, data type specifies the type of data a variable can hold. It determines how the data is stored in memory and what operations can be performed on it.

Python is dynamically typed, meaning you don’t need to declare a variable’s data type explicitly; the type is inferred automatically based on the value assigned to the variable.

Built-in Data Types in Python

Here’s a table of built-in data types in Python:

CategoryData Type
Numericint, float, complex
Stringstr
Sequencelist, tuple, range
Mappingdict
Booleanbool
Setset, frozenset
Binarybytes, bytearray, memoryview
NoneNoneType

In Python, everything is an object, and all data types are instances (objects) of predefined classes, meaning integers, strings, lists, and even None and bool are objects created from their respective classes.

(1) Python Numeric Data Type

In Python, numeric data types are used to store numeric values. Python supports three main types of numeric data: int, float and complex

(a) int (Integer)

In Python, int (integer) data type is used to represent whole numbers, either positive or negative, without a fractional part.

For example:

x = 10
y = -22
print(type(x))  # <class 'int'>
print(type(y))  # <class 'int'>

You can get the data type of a variable in Python using the type() function.

(b) float (Floating-point)

In Python, float data type is used to represent real numbers that contain a decimal point or are expressed in exponential form.

For example:

x = 5.72
y = 3e2
print(type(x))  # <class 'float'>
print(type(y))  # <class 'float'>

(c) complex (Complex Numbers)

In Python, complex data type is used to represent complex numbers of the form a + bj, where a is the real part and b is the imaginary part, and j is the imaginary unit.

For example:

complex_num = 3 + 4j
print(type(complex_num))  # <class 'complex'>

(2) Python Text (String) Data Type

In Python, text data is represented using the string data type, abbreviated as str. A string is a sequence of characters enclosed in single, double, or triple quotes. It is commonly used to represent text such as words, sentences, or paragraphs.

For example:

name = "James Bond"
message = "The name is Bond, James Bond"

print(type(name))    # <class 'str'>
print(type(message))  # <class 'str'>

(3) Python Sequence Data Type

Sequence data types in Python are used to store collections of items in a specific order. The main built-in sequence types are: Lists and Tuples

(a) Lists

A list is an ordered, mutable collection of items that can hold elements of different data types.

For example:

users = ["James Bond", "Ethan Hunt", "Tony Stark"]
print(type(users))    # <class 'list'>
print(users[1])       # Ethan Hunt

You can access items in a list using their index. Indexing starts at 0 for the first item.

(b) Tuples

A tuple is an ordered, immutable collection of items that can hold elements of different data types.

For example:

users = ("James Bond", "Ethan Hunt", "Tony Stark")
print(type(users))    # <class 'tuple'>
print(users[1])       # Ethan Hunt

Like lists, you can access items in a tuple using their index. Indexing starts at 0 for the first item.

The main difference between tuples and lists is that tuples are immutable, meaning their contents cannot be changed after creation, while lists are mutable and can be modified.

(4) Python Mapping (Dictionary) Data Type

Dictionaries are unordered collections of key-value pairs, where each key must be unique within the dictionary.

For example:

person = {'name': 'James Bond', 'age': 37, 'city': 'London'}
print(type(person))   # <class 'dict'>
print(person["age"])  # 37

You can access items in a dictionary using keys.

(5) Python Boolean Data Type

In Python, the bool data type represents Boolean values, either True or False. These are often used in conditional statements and expressions to control the flow of a program.

For example:

is_raining = True
is_sunny = False
print(type(is_raining))  # <class 'bool'>
print(type(is_sunny))    # <class 'bool'>

(6) Set Data Type

A set in Python is an unordered collection of unique elements. It is useful when you need to eliminate duplicate entries or perform mathematical set operations like unions, interactions, and differences.

For example:

my_set = {1, 2, 3}
print(type(my_set))  # <class 'set'>

Frozenset Data Type

In Python, a frozenset is an immutable version of the set data type. It is similar to a regular set, but once created, its elements cannot be modified. This makes it useful for situations where you need to ensure the integrity of a set and prevent accidental changes.

For example:

my_frozenset = frozenset([1, 2, 3])
print(type(my_frozenset))  # <class 'frozenset'>

(7) Binary Data Type

In Python, the binary data types are used to represent data in a binary format. They are particularly useful for handling raw binary data such as image files, audio files, or any data where byte-level operations are required.

The two main binary data types in Python are:

  • bytes
  • bytearray

(a) bytes Data Type

The bytes data type in Python represents an immutable sequence of byte values (integers between 0 and 255). It is used to store and manipulate raw binary data, such as in file I/O, network protocols, or data encoding. Since bytes are immutable, once it is created, its content cannot be changed.

For example:

byte_data = b"James Bond"
print(type(byte_data))  # <class 'bytes'>

# Accessing individual bytes
print(byte_data[0])  # 74 (ASCII value of 'J')

(b) bytearray Data Type

The bytearray data type in Python is a mutable sequence of bytes. It is similar to the bytes data type, but unlike bytes, it can be modified after it is created. This makes it useful for situations where you need to manipulate binary data dynamically.

For example:

my_bytearray = bytearray(b"James Bond")
print(type(my_bytearray))  # <class 'bytearray'>

# Modifying an element
my_bytearray[0] = 76  # Change 'J' (74) to 'L' (76)

print(my_bytearray)   # bytearray(b'Lames Bond')

(c) memoryview Data Type

The memoryview data type in Python is used to provide direct access to the internal memory of an object without copying the data. It allows you to efficiently work with binary data stored in objects like bytes, bytearray or other objects that support the buffer protocol.

For example:

# Creating memoryview from bytearray

my_bytearray = bytearray(b"James Bond")
my_memoryview = memoryview(my_bytearray)
print(type(my_memoryview))  # <class 'memoryview'>

# Accessing elements in memoryview
print(my_memoryview[0])   # Output 74 (ASCII value of 'J')

(8) None Data Type

In Python, None is a special constant that represents the absence of a value or a null value. It is an object of its own data type, NoneType.

For example:

x = None
print(type(x))   # <class 'NoneType'>