Comprehensive Overview of 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, usage examples, and best‑practice considerations for efficient programming.
1. Numbers
Python supports three primary numeric types: int, float, and complex. Since Python 3, integers have unlimited size, and floats follow the IEEE‑754 double‑precision standard.
integer = 42</code>
<code>floating_point = 3.14</code>
<code>complex_number = 1 + 2j</code>
<code>print(type(integer)) # </code>
<code>print(type(floating_point)) # </code>
<code>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 sequences of characters defined with single, double, or triple quotes and are immutable. Python offers many built‑in methods for concatenation, formatting, searching, etc.
single_quoted = 'Hello'</code>
<code>double_quoted = "World"</code>
<code>triple_quoted = '''This is a </code>
<code>multi-line string'''</code>
<code># 字符串方法</code>
<code>formatted_string = f"Combine {single_quoted} and {double_quoted}"</code>
<code>print(formatted_string) # 输出: Combine Hello and World3. 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]</code>
<code># 列表操作</code>
<code>my_list.append(5)</code>
<code>print(my_list[0]) # 输出: 1</code>
<code>print(my_list[-1]) # 输出: 5</code>
<code>print(my_list[1:4]) # 输出: [2, 3, 'four']4. Tuples
Tuples are similar to lists but immutable; they are suitable for fixed collections and ensure data integrity.
my_tuple = (1, 2, 3, 'four')</code>
<code># 元组解包</code>
<code>a, b, c, d = my_tuple</code>
<code>print(d) # 输出: four5. Dictionaries
Dictionaries are unordered key‑value mappings where keys must be unique and immutable. They provide fast lookup, insertion, and deletion.
my_dict = {"name": "Alice", "age": 30}</code>
<code># 字典操作</code>
<code>print(my_dict["name"]) # 输出: Alice</code>
<code>my_dict["city"] = "Beijing"</code>
<code>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}</code>
<code>another_set = {3, 4, 5}</code>
<code># 集合运算</code>
<code>union_set = my_set | another_set # 并集</code>
<code>intersection_set = my_set & another_set # 交集</code>
<code>difference_set = my_set - another_set # 差集7. Booleans
Booleans have two values, True and False, and are primarily used for conditional logic.
is_valid = True</code>
<code>if is_valid:</code>
<code> print("Valid!")</code>
<code>else:</code>
<code> print("Invalid!")8. None Type
None represents the absence of a value and is often used for variable initialization or as a default return.
result = None</code>
<code>print(result is None) # 输出: True9. Sequences
Sequences are a family of data structures that include lists, tuples, and strings, sharing operations like indexing, slicing, and membership testing.
sequence = [1, 2, 3, 4]</code>
<code>print(sequence[1]) # 输出: 2</code>
<code>print(sequence[1:3]) # 输出: [2, 3]</code>
<code>print(2 in sequence) # 输出: True10. Mappings
Mappings associate keys with values; the most common example is a dictionary, enabling fast key‑based lookup.
11. Iterables
Any object that can be looped over with a for‑loop is iterable, including lists, tuples, strings, dictionaries, sets, and generators.
iterable = [1, 2, 3]</code>
<code>for item in iterable:</code>
<code> print(item)</code>
<code># 生成器表达式</code>
<code>gen = (x * x for x in range(4))</code>
<code>for num in gen:</code>
<code> print(num)12. Files
File handling in Python is straightforward using the built‑in open() function and context managers for automatic resource cleanup.
with open('example.txt', 'w') as file:</code>
<code> file.write('Hello, world!')</code>
<code>with open('example.txt', 'r') as file:</code>
<code> content = file.read()</code>
<code> print(content)Why Understanding Data Types Matters
Choosing the appropriate data type can improve performance (e.g., set membership tests are faster than list scans), enhance code readability, aid memory management—especially with large data sets—and ensure smooth integration with third‑party libraries and frameworks.
Conclusion
Mastering Python's data types and their characteristics is fundamental for becoming an efficient programmer; continuous practice enables you to write robust, maintainable code and discover advanced usage patterns.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
