What is the difference between list and tuple in Python?

103 views

What is the difference between list and tuple in Python?

What is the difference between list and tuple in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • List:
    • Mutable (can be modified after creation).
    • Enclosed in square brackets [].
    • Ideal for storing collections of items that might change over time.
  • Tuple:
    • Immutable (cannot be modified after creation).
    • Enclosed in parentheses ().
    • Useful for storing data that should not change, like coordinates or settings.

Example:

my_list = [1, 2, 3]
my_tuple = (1, 2, 3)

# You can modify a list:
my_list[0] = 100  # Now my_list is [100, 2, 3]

# You cannot modify a tuple (this will throw an error):
my_tuple[0] = 100  # TypeError: 'tuple' object does not support item assignment

solveurit24@gmail.com Changed status to publish February 13, 2025
0