❌

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 …

Python Task – Tuple

2 August 2024 at 03:42
  1. Create a tuple containing the names of three fruits. Print the tuple and its type.
  2. Given a tuple t = ("apple", "banana", "cherry"), access and print the second element.
  3. Unpack the tuple t = (1, 2, 3) into variables a, b, and c, and print the variables.
  4. Concatenate two tuples t1 = (1, 2) and t2 = (3, 4) and print the result.
  5. Create a tuple t = ("repeat",) and repeat it three times. Print the resulting tuple.
  6. Given a tuple t = (1, 2, 3, 2, 2, 4), count the number of times the number 2 appears.
  7. Given a tuple t = ("a", "b", "c", "d"), find the index of the element "c".
  8. Check if the element 5 is present in the tuple t = (1, 2, 3, 4)
  9. Find and print the length of the tuple t = ("one", "two", "three").
  10. Slice the tuple t = (0, 1, 2, 3, 4, 5) to obtain a sub-tuple containing only the elements from index 2 to 4.
  11. Create a nested tuple representing a 2D point (x, y) = ((1, 2), (3, 4)). Access and print the second coordinate of the second point.
  12. Try to change the value of the first element in the tuple t = (1, 2, 3) and observe what happens.
  13. Convert a list l = [1, 2, 3] to a tuple and print the result. Then convert a tuple t = (4, 5, 6) to a list and print the result.
  14. Create a tuple with a single item 5 and verify its type is a tuple.
  15. Iterate over the tuple t = ("ParottaSalna", "is", "good") and print each element.
  16. Convert the string "hello" into a tuple of characters.
  17. Convert a dictionary d = {"one": 1, "two": 2} into a tuple of its items.
  18. Write a function that takes a tuple of numbers and returns the sum of the numbers.
  19. Use tuples as keys in a dictionary to represent points on a grid. For example, grid = {(0, 0): "origin", (1, 2): "point A"}.

❌
❌