Fundamentals 8 min read

Comprehensive Guide to Python Built‑in Data Types

This article provides a comprehensive overview of Python's built-in data types—including numbers, strings, lists, tuples, dictionaries, sets, booleans, None, sequences, mappings, iterables, and file handling—explaining their characteristics, typical use cases, and offering clear code examples for each.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Comprehensive Guide to Python Built‑in Data Types

1. Numbers Python supports three primary numeric types: int, float, and complex. Integers have unlimited size, while floats follow the IEEE‑754 double‑precision standard.

integer = 42
floating_point = 3.14
complex_number = 1 + 2j
print(type(integer))      #
print(type(floating_point)) #
print(type(complex_number)) #

Integers represent whole numbers, floats represent real numbers with a fractional part, and complex numbers consist of a real and an imaginary part (ending with j).

2. Strings Strings are immutable sequences of characters defined with single, double, or triple quotes. Python offers many built‑in methods for concatenation, formatting, searching, etc.

single_quoted = 'Hello'
double_quoted = "World"
triple_quoted = '''This is a
multi-line string'''
# 字符串方法
formatted_string = f"Combine {single_quoted} and {double_quoted}"
print(formatted_string)  # 输出: Combine Hello and World

3. Lists Lists are ordered, mutable collections that may contain duplicate items of heterogeneous types. They support indexing, slicing, and methods such as append() , extend() , pop() , etc.

my_list = [1, 2, 3, 'four', True]
# 列表操作
my_list.append(5)
print(my_list[0])   # 输出: 1
print(my_list[-1])  # 输出: 5
print(my_list[1:4]) # 输出: [2, 3, 'four']

4. Tuples Tuples are immutable ordered collections, useful for fixed data groups and ensuring data integrity.

my_tuple = (1, 2, 3, 'four')
# 元组解包
a, b, c, d = my_tuple
print(d)  # 输出: four

5. Dictionaries Dictionaries are unordered key‑value mappings; keys must be unique and immutable, while values can be any type. They provide fast lookup, insertion, and deletion.

my_dict = {"name": "Alice", "age": 30}
# 字典操作
print(my_dict["name"])  # 输出: Alice
my_dict["city"] = "Beijing"
del my_dict["age"]

6. Sets Sets are unordered collections of unique elements, implemented via hash tables, supporting mathematical operations like union, intersection, and difference.

my_set = {1, 2, 3}
another_set = {3, 4, 5}
# 集合运算
union_set = my_set | another_set  # 并集
intersection_set = my_set & another_set  # 交集
difference_set = my_set - another_set  # 差集

7. Booleans Booleans have two values, True and False , and are primarily used in conditional statements and logical operations.

is_valid = True
if is_valid:
print("Valid!")
else:
print("Invalid!")

8. None Type None represents the absence of a value and is often used for variable initialization or as a default return value.

result = None
print(result is None)  # 输出: True

9. Sequences Sequences are a generic term for ordered collections such as lists, tuples, and strings, sharing operations like indexing, slicing, and membership testing.

sequence = [1, 2, 3, 4]
print(sequence[1])     # 输出: 2
print(sequence[1:3])   # 输出: [2, 3]
print(2 in sequence)   # 输出: True

10. Mappings Mappings associate keys with values; the most common example in Python is the dictionary, enabling fast key‑based lookups.

11. Iterables Any object that can be used in a for loop—such as lists, tuples, strings, dictionaries, sets, or generators—is iterable.

iterable = [1, 2, 3]
for item in iterable:
print(item)
# 生成器表达式
gen = (x * x for x in range(4))
for num in gen:
print(num)

12. Files File objects allow reading, writing, or appending to files; using a context manager ( with ) ensures automatic closure.

with open('example.txt', 'w') as file:
file.write('Hello, world!')
with open('example.txt', 'r') as file:
content = file.read()
print(content)

Understanding these data types is essential for performance optimization, code clarity, resource management, and effective integration with libraries and frameworks.

Mastering Python's data types forms the foundation for writing robust, maintainable, and efficient code.

PythonData Typescode examplesTutorialprogramming fundamentals
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.