A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Ways to create a tuple:

Empty Tuple:

Even for a tuple with a single value, comma needs to be included:

Accessing, Updating, Deleting and Basic Operations on Tuples

  • Accessing of tuple is similar to the way you access lists.
  • Tuples are immutable, hence, updating it is not possible. However, you can concatenate two tuples using ā€œ+ā€ operator to form a new tuple.
  • Same way, deletion of tuple elements is also not possible.
  • All basic operations that can be performed in lists can also be performed on tuples.

Built-in Functions of Tuples

cmp(tuple1, tuple2)
Compares elements of both tuples.

Gives the total length of the tuple.

Returns item from the tuple with max value.

Returns item from the tuple with min value.

Converts a list into tuple.

We will now see an example which covers all the functions of tuples.

Click here to Copy the Code

The output of the above code is:

Click here to Copy the Code

Methods of Tuples: Tuples has all the methods that are their with lists. Though, keeping in mind its immutable nature not all methods of list will work. Only those methods which keeps its immutability intact will only work.

Moreover, we need to still have a discussion on its mutability which we will do once all the sequences are covered as mentioned in my previous post. So, that’s all for tuples, will catch you all in my next post on Dictionaries in Python.