Python Tutorials-Tuples

In Python Tuples and Lists are almost identical. The primary difference is Tuples cannot be manipulated. That means you cannot add, remove or change a Tuple, that is to say Tuples are immutable. The idea is that you may have information in your program that you don’t want to change. Python recognizes that some times as programmers we make mistakes so rather than take a chance at changing something that you don’t want to change, you can use Tuples.

How does a Tuple work?

This will output 1, 2, 3, 4, 5. Which is exactly a list would do. The difference is if I did the following:

I will get an error. That’s because Tuples cannot be appended to, it is immutable.

If you’re confused by how Python distinguishes between a List and a Tuple, remember that Lists are surrounded by [] and Tuples are surrounded by ().

You will get the same result for myTuple and myList, the difference is that if I want to I can change myList. You can however change a Tuple to a List. Here’s how:

The output should be 1, 2, 3, 4, 5, 6.

That’s it for Tuples. Practice, Practice, Practice