UnicMinds

Tuples in Python - Basics

Tuples in Python – Basics

Tuple is one of the built-in data types in Python. A Python tuple is a sequence of comma separated items, enclosed in parentheses (). The items in a Python tuple need not be of the same data type. One thing that separates tuples apart is that tuples are immutable, they cannot be changed.

Following are some examples of Python tuples.

tup1 = (“a”, “b”, 1, 2)

tup2 = (1, 2, 3, 4, 5)

tup3 = (“a”, “b”, “c”, “d”)

Creating Empty Tuples

The empty tuple is written as two parentheses containing nothing.

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though there is only one value.

tup1 = (50,);

In Python, tuple is a sequence data type. It is an ordered collection of items. Each item in the tuple has a unique position index, starting from 0.

In a C/C++/Java array, the array elements must be of the same type. On the other hand, Python tuples may have objects of different data types.

Python tuple and list both are sequences. One major difference between the two is, Python list is mutable, whereas tuple is immutable. Although any item from the tuple can be accessed using its index, and cannot be modified, removed or added.

Tuple Functions

Accessing tuple elements

One can access tuple items by referring to the index number, inside square brackets as shown below:

Tuple Basics in Python

Output: B

Negative indexing means starting from the end of the tuple as shown below:

-1 refers to the last item, -2 refers to the second last item etc.

Negative Index - Tuples in Python

Output: C

One can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items.

Tuples in Python - UnicMinds

Updating the values in tuples

Even though one cannot change, add, or remove items once the tuple is created,there are some workarounds. One can convert the tuple into a list, change the list, and convert the list back into a tuple as shown below.

Tuple in python - converting into list and changing items

Adding elements in a tuple can also be done in a similar way. One can convert it into a list, add your element, and convert it back into a tuple.

Appending elements in a tuple in Python

Removing elements in a tuple can also be done in a similar way. One can convert it into a list, delete your element, and convert it back into a tuple.

Removing elements in a tuple

Or you can delete the tuple completely:

Deletion of Tuple

In this post, we’ve covered the basic introduction to tuples, how to create them, use them, and some common operations involving tuples. Practice working with various types of tuples – create them in your programs and use various functions such as len() and count() so that you become familiar with the basics of tuples in Python programming. 

Have fun coding!

Hope this is useful, thank you.

You may like to read: Coding Principles for Kids, Programming a Simple Cookie Clicker Game in Scratch, & What is a Machine Word?

Leave a Comment

Your email address will not be published. Required fields are marked *

BOOK A FREE TRIAL