Sobes.tech
Junior — Middle

Can you explain the main data structures used in Python?

sobes.tech AI

Answer from AI

In Python, there are several core data structures widely used for storing and processing data:

  • Lists (list) — ordered mutable collections that can contain elements of different types. They allow adding, removing, and modifying elements.
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
  • Tuples (tuple) — ordered immutable collections. Used when you need to guarantee that data will not change.
point = (10, 20)
  • Sets (set) — unordered collections of unique elements. Useful for operations involving uniqueness and sets.
unique_numbers = {1, 2, 3, 2}  # will be {1, 2, 3}
  • Dictionaries (dict) — unordered collections of key-value pairs. Very convenient for quick access by key.
person = {'name': 'Ivan', 'age': 30}
print(person['name'])  # Ivan

These data structures cover most tasks related to data storage and organization in Python.