Junior — Middle
How do the main data types in Python differ and what are their features?
sobes.tech AI
Answer from AI
In Python, the main data types are divided into mutable and immutable.
- Numbers (int, float, complex) — immutable, used to store numeric values.
- Strings (str) — immutable sequences of characters.
- Lists (list) — mutable ordered collections, can contain elements of different types.
- Tuples (tuple) — immutable ordered collections.
- Sets (set) — mutable unordered collections of unique elements.
- Dictionaries (dict) — mutable collections of key-value pairs.
Features:
- Immutable types are safe to use as dictionary keys or set elements.
- Mutable types allow changing content without creating a new object.
Example:
x = 10 # int
name = "Ivan" # str
lst = [1, 2, 3] # list
tpl = (1, 2, 3) # tuple
s = {1, 2, 3} # set
d = {"key": "value"} # dict