MediumPython — Collections — 3— Tuple. this article gives you complete… | by Faheem unnisa | Medium
Sitemap
Faheem unnisa

Faheem unnisa

Data Analytics | Marketing | AI | LIFE | Sql, Python ,Mathmatics, AI , ML , Tableau my Fav https://www.linkedin.com/in/faheemunnisa/

Python — Collections — 3— Tuple

3 min readDec 11, 2024

--

this article gives you complete understanding of Tuple

Tuples are one of Python’s fundamental data structures, used to store a collection of items. While similar to lists, tuples have some key differences that make them unique and suitable for specific use cases.

Get Faheem unnisa’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s dive deep into understanding tuples, their properties, and practical applications.

Press enter or click to view image in full size

What is a Tuple?

A tuple is an immutable sequence in Python, meaning that once created, its values cannot be changed. Tuples are enclosed in parentheses (()) and can store elements of different data types (heterogeneous).

Key Features of Tuples

  1. Immutable: Once defined, the elements cannot be altered (no add, remove, or update operations).
  2. Ordered: Tuples maintain the order of their elements.
  3. Heterogeneous: Can store elements of different types (e.g., integers, strings, lists).
  4. Hashable: Can be used as keys in dictionaries if they contain only hashable types.

How to Create a Tuple?

1. Basic Tuple Creation

Tuples are created by placing values in parentheses, separated by commas.

# Creating a tuple
my_tuple = (1, 2, 3, "Faheem", True)
print(my_tuple) # Output: (1, 2, 3, 'Faheem', True)

2. Single-Element Tuple

For a tuple with a single element, include a trailing comma.

single_tuple = (42,)
print(single_tuple) # Output: (42,)

Tuple Operations

1. Accessing Elements

Use indexing to access individual elements (0-based).

my_tuple = (10, 20, 30)
print(my_tuple[0]) # Output: 10
print(my_tuple[-1]) # Output: 30

2. Slicing

Extract a portion of the tuple using slicing (start:end:step).

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4]) # Output: (2, 3, 4)
print(my_tuple[::-1]) # Output: (5, 4, 3, 2, 1) (reversed)

3. Concatenation and Repetition

Combine tuples or repeat their elements.

t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2) # Output: (1, 2, 3, 4) (concatenation)
print(t1 * 3) # Output: (1, 2, 1, 2, 1, 2) (repetition)

Key Tuple Methods

MethodDescriptionExamplecount()Returns the number of occurrences of a value(1, 2, 2).count(2) → 2index()Returns the first index of a value(1, 2, 3).index(2) → 1

Immutability of Tuples

Once created, a tuple cannot be modified. However, if it contains mutable elements (like a list), those elements can be modified.

nested_tuple = (1, [2, 3], 4)
nested_tuple[1][0] = 42
print(nested_tuple) # Output: (1, [42, 3], 4)

When to Use Tuples?

  1. When immutability is important: Tuples prevent accidental modification of data.
  2. As dictionary keys: Only immutable types (like tuples) can be used as keys in dictionaries.
  3. For fixed collections: Represent a sequence of elements that won’t change.

Example: Database Records

In a financial project, tuples can be used to represent static data that should not change, such as currency exchange rates, fixed asset details, or immutable records.

Scenario: Currency Exchange Rates

Imagine a system that tracks currency exchange rates for a financial application. Since exchange rates for a particular day are fixed once fetched, they can be stored as tuples.

# Tuple representing exchange rates (currency, rate)
exchange_rates = (
("USD", 1.0), # US Dollar (Base)
("EUR", 0.85), # Euro
("GBP", 0.75), # British Pound
("JPY", 110.57), # Japanese Yen
("INR", 74.30), # Indian Rupee
)

# Accessing exchange rate for a specific currency
for currency, rate in exchange_rates:
if currency == "EUR":
print(f"The exchange rate for EUR is {rate}")

Explanation:

  • Why Tuple?
  • Exchange rates do not change frequently, so immutability ensures the data remains intact.
  • It’s lightweight and efficient for such read-only operations.

Summary

Press enter or click to view image in full size

If you are wondering, What are the diffrence between List or Tuple

Press enter or click to view image in full size

--

--

Written by Faheem unnisa

Data Analytics | Marketing | AI | LIFE | Sql, Python ,Mathmatics, AI , ML , Tableau my Fav https://www.linkedin.com/in/faheemunnisa/