Posts

Showing posts from September, 2025

Why Did Python Give a TypeError When I Tried to Change My Tuple?

You've gotten comfortable with Python lists, using square brackets [] to store collections of items. Then, you encounter something that looks almost identical but uses parentheses () —a tuple . You create one, and it seems to work just like a list. You can access items by their index, you can loop through it, and you can check its length. Python # A tuple looks a lot like a list my_coordinates = ( 10 , 20 , 30 ) print( f"The first value is: {my_coordinates[ 0 ]} " ) # Works great! Then, you try to change one of the values. Maybe you want to update the first coordinate to 15 . You write the code just like you would for a list: my_coordinates[0] = 15 And your program immediately crashes with this error: TypeError: 'tuple' object does not support item assignment What does this mean? Why does it work for a list but not a tuple? This isn't a bug; it's the single most important feature of a tuple. Understanding this difference will help you write safer and mo...