Python Data Types: Core Structures and Operations

Pyhton's type system provides immutable primitives and mutable collections for diverse programming needs. Understanding their behaviors, performance characteristics, and interaction patterns enables efficient data manipulation.

Numeric Types

Integers (int) represent whole numbers without magnitude constraints in Python 3. Floating-point numbers (float) handle decimal representations, while complex numbers (complex) store mathematical complex values with real and imaginary components.

Arithmetic operations follow standard mathematical conventions:

calculated = ((15 + 3) * 2 - 8) / 4  # Standard precedence
remainder = 17 % 5                    # Modulo operation
quotient = 17 // 5                    # Floor division
power = 2 ** 10                       # Exponentiation

Type coercion between numeric types requires explicit conversion:

price = 29.99
cents = int(price)        # Truncates to 29 (floor toward zero)
cost = float(100)         # Converts to 100.0

Boolean values (bool) are implemented as a subclass of int, where True equals 1 and False equals 0. This enables arithmetic with boolean expressions:

flags = [True, False, True]
total = sum(flags)        # Results in 2

# Boolean conversion from numeric context
print(bool(0))            # False
print(bool(-5))           # True (any non-zero)

String Manipulation

Strings are immutable sequences of Unicode characters. They support indexing, slicing, and rich method collections without modifying the original object.

Indexing accepts both positive (left-to-right) and negative (right-to-left) positions:

code = "PYTHON-3.11"
first_char = code[0]      # 'P'
last_char = code[-1]      # '1'

Slicing extracts substrings using [start:stop:step] notation, where the stop index remains exclusive:

text = "DataScience"

# Basic slicing
prefix = text[0:4]        # 'Data' (indices 0,1,2,3)
suffix = text[4:]         # 'Science' (from index 4 to end)
clone = text[:]           # Full copy

# Extended slicing with step
every_second = text[::2]  # 'DtScine'
reversed_text = text[::-1] # 'ecneicSataD'

String methods provide transformation utilities:

raw_input = "  user@EXAMPLE.com  "
clean = raw_input.strip().lower()  # "user@example.com"

# Case conversions
label = "hello world"
title_case = label.title()         # "Hello World"
swapped = label.swapcase()         # "HELLO WORLD"

# Searching and validation
if label.startswith("he") and "world" in label:
    position = label.find("world")  # Returns 6, -1 if absent

Formatting approaches include positional, indexed, and named parameter substitution:

# Positional
"{} scored {} points".format("Alice", 95)

# Indexed with reuse
"{0} vs {1} (Score: {0}-{2})".format("TeamA", "TeamB", 3)

# Named parameters for clarity
"{player} achieved {score} in {game}".format(
    player="Mario", score=5000, game="Level-1"
)

Sequential Collections

Tuples are immutable ordered sequences. While the container cannot change, mutable elements within remain modifiable:

record = ("alpha", 42, ["pending", "review"])
# record[0] = "beta"  # TypeError: immutable
record[2].append("approved")  # Valid: modifies nested list

The range type generates arithmetic progressions efficiently without storing all values in memory:

# range(start, stop, step)
indices = range(5)            # 0, 1, 2, 3, 4
decade = range(10, 0, -2)     # 10, 8, 6, 4, 2

Lists provide mutable, ordered storage supporting heterogeneous types. They offer extensive modification methods:

inventory = ["sword", "shield"]

# Addition methods
inventory.append("potion")           # Add single item
inventory.extend(["map", "key"])     # Add multiple items
inventory.insert(1, "armor")         # Insert at index

# Removal strategies
removed = inventory.pop()            # Remove and return last
inventory.pop(0)                     # Remove by index
inventory.remove("shield")           # Remove by value (first match)

# Slice assignment replaces ranges
inventory[1:3] = ["bow", "arrow"]    # Replace 2 items with 2 items
inventory[1:3] = []                  # Delete range (equivalent to del)

List sorting operates in-place or generates new sequences:

scores = [88, 92, 75, 65, 99]
scores.sort()                        # Ascending in-place
scores.sort(reverse=True)            # Descending
scores.reverse()                     # In-place reversal

# Non-mutating sort
ranked = sorted(scores, reverse=True)

String-list conevrsion bridges text processing and data structures:

# Splitting text into lists
csv_line = "alpha,beta,gamma"
tokens = csv_line.split(",")         # ['alpha', 'beta', 'gamma']

# Joining lists into strings
words = ["Python", "Data", "Science"]
sentence = " ".join(words)           # "Python Data Science"
path = "/".join(["home", "user", "docs"])  # "home/user/docs"

Mapping Types

Dictionaries store key-value associations with unique, hashable keys and arbitrary values. Hashability requires immutability (strings, numbers, tuples of immutables).

Dictionary operations include:

profile = {"id": 101, "name": "Chen", "active": True}

# Access patterns
user_id = profile["id"]              # Raises KeyError if missing
email = profile.get("email", "N/A")  # Safe access with default

# Modification
profile["role"] = "admin"            # Add or update
profile.update({"email": "chen@ex.com", "active": False})

# Conditional insertion
profile.setdefault("visits", 0)      # Only sets if key absent

Dictionary views provide dynamic reflections of content:

keys = profile.keys()
values = profile.values()
pairs = profile.items()              # Tuple (key, value) iterations

# Unpacking during iteration
for field, data in profile.items():
    print(f"{field}: {data}")

Nested structures require careful navigation:

data = {
    "users": ["anna", "bob"],
    "metadata": {"count": 2, "version": 1.0}
}

# Modifying nested elements
data["users"].append("charlie")
data["metadata"]["version"] = 1.1

Set Operations

Sets store unique, unordered elements supporting mathematical set operations. Elements must be hashable (immutable).

set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}

# Membership and addition
if 3 in set_a:
    set_a.add(9)                     # Ignored if 9 exists

# Mathematical operations
common = set_a & set_b               # Intersection: {4, 5}
union = set_a | set_b                # Union: {1, 2, 3, 4, 5, 6, 7, 8}
diff = set_a - set_b                 # Difference: {1, 2, 3}
symmetric = set_a ^ set_b            # Symmetric difference

# Set relationships
is_subset = set_a < set_b            # Proper subset check
is_superset = set_a > {1, 2}         # Proper superset

Sets enable efficient duplicate removal:

raw_data = [1, 2, 2, 3, 3, 3]
unique_items = list(set(raw_data))   # [1, 2, 3] (order not preserved)

For immutable set requirements (dictionary keys or set elements), frozenset provides a hashable alternative:

frozen = frozenset(["a", "b", "c"])
# frozen.add("d")  # AttributeError: immutable

Tags: python data-types programming strings Lists

Posted on Thu, 07 May 2026 04:47:06 +0000 by mrdamien