What's the difference between () vs [] vs {}?

后端 未结 4 772
轻奢々
轻奢々 2020-12-13 04:56

What\'s the difference between () vs [] vs {} in Python?
They\'re collections? How can I tell when to use which?

4条回答
  •  孤街浪徒
    2020-12-13 05:24

    • () is a tuple: An immutable collection of values, usually (but not necessarily) of different types.
    • [] is a list: A mutable collection of values, usually (but not necessarily) of the same type.
    • {} is a dict: Use a dictionary for key value pairs.

    For the difference between lists and tuples see here. See also:

    • Python Tuples are Not Just Constant Lists

提交回复
热议问题