Sobes.tech
Middle

What is the difference between a tuple and an array?

sobes.tech AI

Answer from AI

A tuple and an array are data structures, but they differ in several key aspects:

  • Mutability:

    • A tuple is usually immutable, meaning its contents cannot be changed after creation.
    • An array, on the other hand, is mutable, allowing elements to be changed by index.
  • Element types:

    • In a tuple, elements can be of different types.
    • In an array, all elements are usually of the same type (especially in languages with strict typing).
  • Usage:

    • Tuples are often used to group a fixed set of values, such as coordinates (x, y).
    • Arrays are used to store sequences of data where the ability to modify and iterate over elements is important.

Example in Python:

# Tuple
point = (10, 20)  # immutable

# Array (list in Python)
numbers = [1, 2, 3]
numbers[0] = 10  # can be changed