Tuples Demystified : The Power of Immutable Data in Python

Manish KumarManish Kumar
3 min read

Introduction

If you are learning Python two most important data structures you should be familiar are list and tuples. This datatypes are crucial to become efficient Python Developer or if you want to pursue career in Data Science. Both data structures are utilise to store heterogenous data type but the difference between list and tuples i.e. tuples are immutable data type. Let’s deep dive into the exciting journey of tuples.

What are Tuples in Python

Tuples are ordered collections of items that can be of any data type, but are immutable meaning they cannot be modified once created.

How Tuples are differ from other Python Data Structures

  • Immutability: So, one the key differences between tuples and other Python Data Structures is that tuples are immutable. Once a tuple is created then their content cannot be modified. In contrast dictionaries and lists are mutable, meaning their contents can be modified once created.

  • Ordering: Tuples are immutable and their ordered cannot be changed as comparison to lists where ordered can be modified by adding and removing elements.

  • Size: Tuples are much more memory efficient as compared to lists because of its immutable nature interpreter can allocate fixed sized memory since it knows size cannot change whereas list is mutable so interpreter has to allocated extra memory.

  • Speed: Tuples are faster than lists in certain operations such as indexing, accessing and unpacking. Tuples are immutable that gives advantage over lists because list are mutable and interpreter has to allocate extra memory as comparison to fixed memory allocation.

How to create and access tuples in Python ?

1. Initialization of Tuple

2. Access the elements and Slicing Operations

3. How to use operators with tuples ?

  1. Concatenation: The + operator is used to concatenate two or more tuples.

  2. Repetition: The use of * operator is to repeat tuples into specified number of times.

  3. Membership: The in and not operator is used to check whether elements are present in tuples or not.

  4. Comparison: The comparison operator (==, >=,<=,!=,>,<) are used to compare tuples. Tuples are compared element wise meaning each and every element of two tuples are compared.

  5. Identity: The is and is not operator checks if two tuples pointing to the same object in memory.

    Important Note: It’s important to note that not all operations supported by tuples. For Example addition assignment operator += and others in-place operations are not supported because tuples are immutable.

4. Common Tuples Methods and Functions

  1. Count: This method returns the number of times number appears in the tuple.

  2. Index: This method returns the index of first occurrence of element in the tuple.

  3. Functions

Some Real-World Examples of tuples in Python

  1. Storing Coordinates: Tuples are often used to store coordinates in 2d and 3D space. It is advantageous to store 2D coordinates in a ordered-pair because tuples are immutable , so you can assured that intentionally and unintentionally values cannot be manipulated.

  2. RGB Color Value: In HTML color can be specified using RGB value, and tuples are best choice to store that value because it is immutable so values cannot be changed. For Example: Red Color Value → (255,0,0) if the value is modified accidentally then red color changes to another color. So to secure that data tuples are the best choice.

#ChaiCode

0
Subscribe to my newsletter

Read articles from Manish Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Kumar
Manish Kumar