Python Data Types

In Python, every value has a specific data type that defines the kind of data it represents and the operations that can be performed on it. Python is a dynamically typed language, meaning you don’t need to explicitly declare data types—Python determines them at runtime.

The most common data types in Python include numeric, sequence, set, mapping, boolean, and binary types.

Numeric Types

Numeric types in Python include integers, floating-point numbers, and complex numbers.

Integer (int)

Integers are whole numbers without a decimal point.

For example:

x = 10
y = 20

Floating Point Numbers (float)

Floats are numbers that include a decimal point.

For example:

x = 3.14
y = 7.39

Complex Numbers

Complex numbers represent values that have both a real and an imaginary part, written in the form a+bj, where a is the real part and b is the imaginary part.

For example:

x = 2 + 3j
y = 4 + 9j

Sequence Types

Sequence types in Python are used to store collections of items in a specific order. These include strings (str), lists, and tuples.

Strings (str)

A string is used to represent text in Python.

For example:

message = "hello"

Lists

A list is an ordered, mutable collection of items. This means you can change, add, or remove elements after the list is created.

Lists are defined using square brackets [].

fruits = ["apple", "banana", "mango"]

Tuples

A tuple is similar to a list but immutable, meaning its elements cannot be changed, added, or removed after it is created.

Tuples are defined using parentheses ().

fruits = ("apple", "banana", "mango")

Range

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

# Generates numbers from 0 to 4
for i in range(5):
    print(i) # Output: 0, 1, 2, 3, 4

Mapping Type

Mapping types are data structures that store key-value pairs. The most common one is dictionary (dict).

Dictionary (dict)

A dictionary is a built-in mapping type that stores data as a collection of key-value pairs, where each unique key maps to a corresponding value. Dictionaries are mutable and maintain insertion order starting from Python 3.7.

For example:

person = {"name": "James", "age": 37}

Set Types

Set types represent an unordered collection of unique elements. Set types include sets (set) and frozen sets (frozenset).

Sets (set)

A set is a built-in mutable data type that represents an unordered collection of unique, hashable elements.

For example:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # {1, 2, 3, 4}

Frozen Sets (frozenset)

A fronzenset is an immutable version of a set that contains an unordered collection of unique, hashable elements.

For example:

fset = frozenset([1, 2, 3])

Boolean Type

Boolean type (bool) in Python represents one of two values: True or False.

For example:

is_active = True
is_empty = False
print(type(is_active)) # <class 'bool'>

Binary Types

Binary types are used to represent and manipulate byte-level data, making them essential for tasks such as input/output operations, network communication, and processing non-text data, such as images or audio.

Binary types include bytes, bytearray, and memoryview.

bytes

The bytes type is an immutable sequence of integers, where each element represents a byte with a value between 0 and 255. It is commonly used for handling raw binary data, such as in file I/O, network protocols, or other non-text data formats.

For example:

# Create a bytes object from a string
data = b"hello"
print(type(data))

bytearray

The bytearray type is a mutable sequence of integers, where each element represents a byte with a value between 0 and 255. It is useful for working with binary data that needs to be modified in place, such as in file processing, network communication, or protocol implementation.

For example:

# Create a bytearray from a string
b = bytearray("hello", "utf-8")
print(type(b)) # Output: <class 'bytearray'>

# Modify a byte in place 
b[0] = ord("H")
print(b) # Output: bytearray(b'Hello')

memoryview

The memoryview type is a special object that lets you access the internal data of another object (like bytes or bytesarray) without making a copy. This efficiency makes it essential for high-performance tasks like scientific computing, image processing, and network packet analysis.

# Create a mutable sequence of bytes
data = bytearray("hello world", "utf-8")

# Create a memoryview of the original data
view = memoryview(data)

# Create a slice of a memoryview 
slice_view = view[0:5] # the slice points to "hello"

# Conver the memoryview slice to bytes, then decode to string
print(slice_view.tobytes().decode("utf-8")) # Outut: hello

NoneType

The NoneType represents the absence of value or a null value. It has only one instance: the None object.

For example:

a = None
print(type(a)) # Output: <class 'NoneType'>