Thursday, March 6, 2025

Python lab programs on TUPLES with Explanation

 Sure! Below are some sample Python lab programs that focus on tuples and their various operations. Each program comes with a detailed explanation of how the code executes.


1. Creating and Accessing Elements of a Tuple

Objective: Learn how to create a tuple and access its elements.

# Creating a tuple
tuple_example = (10, 20, 30, 40, 50)

# Accessing elements by index
print(f"First element: {tuple_example[0]}")
print(f"Last element: {tuple_example[-1]}")

Explanation:

  • tuple_example = (10, 20, 30, 40, 50): A tuple is created with five integers.
  • tuple_example[0]: The first element of the tuple is accessed by index 0. Python indexing starts from 0.
  • tuple_example[-1]: Negative indexing is used to access the last element of the tuple.
  • Output:
    • First element: 10
    • Last element: 50

2. Tuple Unpacking

Objective: Understand how to unpack the elements of a tuple into separate variables.

# Creating a tuple
tuple_example = (10, 20, 30, 40)

# Unpacking the tuple
a, b, c, d = tuple_example

print(f"a: {a}, b: {b}, c: {c}, d: {d}")

Explanation:

  • tuple_example = (10, 20, 30, 40): A tuple is created with four integers.
  • a, b, c, d = tuple_example: This is tuple unpacking, where each element of the tuple is assigned to a separate variable a, b, c, and d.
  • Output: Each element of the tuple is printed separately:
    • a: 10, b: 20, c: 30, d: 40

3. Concatenating Tuples

Objective: Learn how to concatenate multiple tuples.

# Creating two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

# Concatenating tuples
result = tuple1 + tuple2
print(f"Concatenated tuple: {result}")

Explanation:

  • tuple1 = (1, 2, 3) and tuple2 = (4, 5, 6): Two tuples are created.
  • tuple1 + tuple2: The + operator is used to concatenate tuple1 and tuple2. This combines the elements of both tuples into a single tuple.
  • Output:
    • Concatenated tuple: (1, 2, 3, 4, 5, 6)

4. Repeating Elements in a Tuple

Objective: Learn how to repeat elements in a tuple using the * operator.

# Creating a tuple
tuple_example = (1, 2, 3)

# Repeating the tuple elements
result = tuple_example * 3
print(f"Repeated tuple: {result}")

Explanation:

  • tuple_example = (1, 2, 3): A tuple is created with three integers.
  • tuple_example * 3: The * operator is used to repeat the tuple three times. This creates a new tuple with the same elements repeated three times.
  • Output:
    • Repeated tuple: (1, 2, 3, 1, 2, 3, 1, 2, 3)

5. Finding the Length of a Tuple

Objective: Determine the number of elements in a tuple.

# Creating a tuple
tuple_example = (10, 20, 30, 40)

# Finding the length of the tuple
length = len(tuple_example)
print(f"Length of the tuple: {length}")

Explanation:

  • tuple_example = (10, 20, 30, 40): A tuple is created.
  • len(tuple_example): The built-in len() function is used to find the length of the tuple, which returns the number of elements in the tuple.
  • Output:
    • Length of the tuple: 4

6. Check if an Element Exists in a Tuple

Objective: Check whether a particular element exists in a tuple.

# Creating a tuple
tuple_example = (10, 20, 30, 40, 50)

# Check if an element exists
if 30 in tuple_example:
    print("30 is in the tuple.")
else:
    print("30 is not in the tuple.")

Explanation:

  • tuple_example = (10, 20, 30, 40, 50): A tuple with five integers is created.
  • 30 in tuple_example: The in operator checks whether the element 30 exists in the tuple.
  • Output:
    • 30 is in the tuple.

7. Nested Tuples

Objective: Understand how to work with nested tuples (tuples inside a tuple).

# Creating a nested tuple
nested_tuple = ((1, 2), (3, 4), (5, 6))

# Accessing elements in a nested tuple
print(f"First tuple: {nested_tuple[0]}")
print(f"Second element of first tuple: {nested_tuple[0][1]}")

Explanation:

  • nested_tuple = ((1, 2), (3, 4), (5, 6)): A tuple containing three nested tuples is created.
  • nested_tuple[0]: This accesses the first tuple (1, 2) inside the nested tuple.
  • nested_tuple[0][1]: This accesses the second element (2) of the first tuple inside the nested tuple.
  • Output:
    • First tuple: (1, 2)
    • Second element of first tuple: 2

8. Converting a List to a Tuple

Objective: Learn how to convert a list into a tuple.

# Creating a list
list_example = [10, 20, 30, 40]

# Converting list to tuple
tuple_example = tuple(list_example)
print(f"Converted tuple: {tuple_example}")

Explanation:

  • list_example = [10, 20, 30, 40]: A list is created with four integers.
  • tuple(list_example): The tuple() function is used to convert the list into a tuple.
  • Output:
    • Converted tuple: (10, 20, 30, 40)

9. Count Occurrences of an Element in a Tuple

Objective: Count how many times an element appears in a tuple.

# Creating a tuple
tuple_example = (10, 20, 30, 20, 40, 20)

# Count occurrences of 20
count = tuple_example.count(20)
print(f"The element 20 appears {count} times.")

Explanation:

  • tuple_example = (10, 20, 30, 20, 40, 20): A tuple is created with some repeated elements.
  • tuple_example.count(20): The count() method counts how many times the element 20 appears in the tuple.
  • Output:
    • The element 20 appears 3 times.

10. Accessing a Tuple Inside a List

Objective: Access a tuple that is an element of a list.

# Creating a list of tuples
list_of_tuples = [(1, 2), (3, 4), (5, 6)]

# Accessing a tuple from the list
tuple_example = list_of_tuples[1]
print(f"Second tuple in the list: {tuple_example}")

Explanation:

  • list_of_tuples = [(1, 2), (3, 4), (5, 6)]: A list containing three tuples is created.
  • list_of_tuples[1]: This accesses the second tuple (3, 4) from the list (remember that Python indexing starts at 0).
  • Output:
    • Second tuple in the list: (3, 4)

Summary of Key Tuple Operations:

  • Tuples are immutable: Once created, elements in a tuple cannot be changed, added, or removed.
  • Indexing and slicing: You can access individual elements or slices of a tuple using indexing.
  • Tuples can be unpacked: You can assign the elements of a tuple to individual variables.
  • Concatenation and repetition: You can concatenate two tuples or repeat a tuple multiple times.
  • Operations: Common operations like finding the length, counting occurrences, and checking for membership are supported.

These lab programs demonstrate basic tuple operations and give you a solid foundation for working with tuples in Python. Let me know if you'd like more examples or further explanations!

2 comments: