Cycle 02
Lab Cycle 2 — Strings, Sequences & Data Structures
Tuple Slicing, Evens Extraction, Concatenation & Extremes
Problem Statement
Consider a tuple t1 = (1, 2, 5, 7, 9, 2, 4, 6, 8, 10). Write a Python program to perform the following operations: a. Print contents of t1 in 2 separate lines such that half values come on one line and the other half in the next line. b. Print all even values of t1 as another tuple t2. c. Concatenate a tuple t2 = (11, 13, 15) with t1. d. Return maximum and minimum value from t1.
def main():
t1 = (1, 2, 5, 7, 9, 2, 4, 6, 8, 10)
print("Original Tuple t1:", t1)
# a. Print in 2 separate lines (half on line 1, half on line 2)
mid = len(t1) // 2
first_half = t1[:mid]
second_half = t1[mid:]
print("\n(a) Splitting t1 into two halves:")
print(" Line 1 (First Half): ", first_half)
print(" Line 2 (Second Half):", second_half)
# b. Print all even values of t1 as another tuple t2
t2_evens = tuple(x for x in t1 if x % 2 == 0)
print("\n(b) Even values of t1 as tuple t2:")
print(" t2 =", t2_evens)
# c. Concatenate tuple t2 = (11, 13, 15) with t1
t2_concat = (11, 13, 15)
concatenated_tuple = t1 + t2_concat
print("\n(c) Concatenation with (11, 13, 15):")
print(" Result =", concatenated_tuple)
# d. Return maximum and minimum value from t1
min_val = min(t1)
max_val = max(t1)
print("\n(d) Extremes of t1:")
print(f" Minimum value = {min_val}")
print(f" Maximum value = {max_val}")
if __name__ == "__main__":
main()
Question 4 of 4