❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

The Magic Photo Album – Python Tuple

3 August 2024 at 09:09

Morning: Creating Snapshots

One sunny morning, the adventurous family decides to document their recent trips. They open their magical photo album and start adding new snapshots.

Let’s create snapshots:


# Snapshots of the family's adventures
ooty_adventure = ("Ooty", "2024-07-01", "Visited Botanical Garden")
munnar_adventure = ("Munnar", "2023-12-15", "Saw Nilgiri Tahr in Eravikulam national park")
manjolai_adventure = ("Manjolai", "2023-05-21", "Enjoyed hill stations")

photo_album = [ooty_adventure, munnar_adventure, manjolai_adventure]

Noon: Recalling Memories

At noon, the family gathers around the table and starts recalling details of their Ooty trip.

Accessing Elements:


# Accessing elements in a tuple
location = ooty_adventure[0]
date = ooty_adventure[1]
description = ooty_adventure[2]

print(f"Location: {location}, Date: {date}, Description: {description}")

# output: Location: Ooty, Date: 2024-07-01, Description: Visited Botanical Garden

Afternoon: Sharing Stories

In the afternoon, they decide to share the details of their Munnar trip by unpacking the snapshot into separate parts.

Unpacking Tuples:


# Unpacking tuple
location, date, description = munnar_adventure
print(f"Location: {location}, Date: {date}, Description: {description}")

After some time, They decided to change the values of an ooty adventure


ooty_adventure[2] = "Visited Doddabeta Peak"

Tuples in Python are immutable, meaning that once a tuple is created, its elements cannot be changed.

However, there are some workarounds to β€œchange” values within a tuple by converting it to a mutable data type, such as a list, making the change, and then converting it back to a tuple.

Evening: Combining Adventures

As evening approaches, the family feels nostalgic and decides to combine the Paris and Kenya adventures into one big story.

Concatenating Tuples:


# Concatenating tuples
combined_adventure = ooty_adventure + munnar_adventure
print(combined_adventure)

Night: Reliving Special Moments

Before going to bed, they want to relive the Japan adventure over and over again, emphasizing how special it was.

Repeating Tuples:


# Repeating tuples
repeated_adventure = manjolai_adventure * 2
print(repeated_adventure)

Midnight: Checking the Album

Late at night, they decide to check if their favorite adventures are still part of their magical photo album.

Checking Membership:


# Checking membership
is_ooty_in_album = ooty_adventure in photo_album
is_munnar_in_album = munnar_adventure in photo_album

print(f"Is the Ooty adventure in the album? {is_ooty_in_album}")
print(f"Is the Munnar adventure in the album? {is_munnar_in_album}")

Counting Their Adventures

They also count the number of adventures they have documented in the album so far.


# Finding length of the tuple
number_of_adventures = len(photo_album)
print(f"Number of adventures in the album: {number_of_adventures}")
# Output: Number of adventures in the album: 3

And then they had a good sleep …

❌
❌