Python Built-in Data Types Explained

Python supports a variety of built-in data types that allow developers to store and manipulate different kinds of information. These include numeric types, text sequences, boolean values, sequecnes, sets, mappings, and binary data.

Numeric Types

Python's numeric category primarily consists of integers (int), floating-point numbers (float), and complex numbers (complex).

To inspect the class of a specific variable, you can use the type() function. For type checking, which considers inheritance, isinstance() is preferred.

value = 20.5

# Returns the class type
print(type(value))  

# Checks if value is an instance of float or int
if isinstance(value, (int, float)):
    print("This is a numeric type")

Strings (Text Sequences)

Strings in Python are sequences of Unicode characters enclosed in either single (') or double (") quotes. They are immutable, meaning individual characters cennot be changed after creation. Strings support slicing operations using the syntax [start:stop].

Indexing can be positive (starting at 0 from the left) or negative (starting at -1 from the right).

text = "programming"

# Access specific index
print(text[0])  # Output: p

# Slice from index 2 to the end
print(text[2:]) # Output: ogramming

# Slice with a step
print(text[::2]) # Output: rgamn

The backslash (\) serves as an escape character. To treat backslashes literally (useful for file paths or regex), prefix the string with r.

# Standard string with escape sequence
print("Line1\nLine2") 

# Raw string
print(r"C:\Users\Name") 

Boolean Type

The boolean type represents logical values and has two possible states: True or False. In Python, booleans are a subclass of integers.

Lists

Lists are ordered, mutable collections defined within square brackets []. They can contain items of varying types.

items = ["apple", 42, 3.14, True]

# Modifying an element
items[0] = "banana"  

# Slicing a list
subset = items[1:3]  # Returns [42, 3.14]

Tuples

Tuples are similar to lists but are immutable. Once defined, their elements cannot be altered. They are defined by enclosing comma-separated values in parentheses ().

coordinates = (10, 20)

# Defining a single-element tuple requires a trailing comma
single = (5,) 

Strings, lists, and tuples are all classified as sequence types in Python.

Sets

A set is an unordered collection of unique elements. Sets are mutable and are defined using curly braces {}. They are useful for membership testing and eliminating duplicate entries.

unique_ids = {1, 2, 3, 2}
print(unique_ids) # Output: {1, 2, 3}

Bytes

The bytes type represents immutable binary data sequences. It is typically created using a b prefix. When accessing elements within a bytes object, integers are retunred.

data = b"binary"
print(data[0])      # Output: 98 (ASCII value for 'b')
print(data[0] == ord('b')) # Output: True

Dictionaries

Dictionaries are mutable, unordered collections of key-value pairs. They are enclosed in curly braces {}, and each key must be unique within the dictionary.

user = {"name": "Alice", "id": 101}

# Constructing a dictionary from a list of tuples
pairs = [("x", 1), ("y", 2)]
new_dict = dict(pairs) # {'x': 1, 'y': 2}

Tags: python Data Types string list Dictionary

Posted on Sat, 08 Aug 2026 16:12:54 +0000 by JNorman