In Python, built-in types can be broadly categorized into two main groups: immutable and mutable types.
Immutable Data Types
These data types are immutable once created. They primarily include:
- Integer (int) - Represents integer values, such as 1, 100, -10, etc.
- Float (float) - Represents floating-point numbers, for example, 1.23, 3.14, etc.
- Boolean (bool) - Represents boolean values, with only True and False.
- String (str) - Used to represent textual data, such as "hello", "Python3", etc.
- Tuple (tuple) - An immutable sequence, for example, (1, 2, 3) or ('a', 'b', 'c').
Mutable Data Types
These data types are mutable after creation. They primarily include:
- List (list) - Used to store sequence data that can be modified, for example, [1, 2, 3] or ['apple', 'banana', 'cherry'].
- Dictionary (dict) - Stores key-value pairs, where keys must be immutable types and values can be of any type, such as {'name': 'Alice', 'age': 25}.
- Set (set) - An unordered collection of unique elements, for example, {1, 2, 3, 4}.
Example
For example, if I need to store student information, I can use a dictionary to represent it:
pythonstudent_info = { 'name': 'John Doe', 'age': 22, 'courses': ['Math', 'Science'] }
In this example, name and age are strings and integers, respectively, while courses is a list, demonstrating how to combine different built-in data types to store and manage complex data structures.
2024年8月9日 09:45 回复