乐闻世界logo
搜索文章和话题

What built-in types are available in Python?

1个答案

1

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:

  1. Integer (int) - Represents integer values, such as 1, 100, -10, etc.
  2. Float (float) - Represents floating-point numbers, for example, 1.23, 3.14, etc.
  3. Boolean (bool) - Represents boolean values, with only True and False.
  4. String (str) - Used to represent textual data, such as "hello", "Python3", etc.
  5. 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:

  1. List (list) - Used to store sequence data that can be modified, for example, [1, 2, 3] or ['apple', 'banana', 'cherry'].
  2. 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}.
  3. 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:

python
student_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 回复

你的答案