Page 276 - CTS - CSA TP - Volume 2
P. 276
COMPUTER SOFTWARE APPLICATION - CITS
TASK 3: Nested tuple
# Read a nested tuple from the keyboard
# Input format: Enter a nested tuple as a string, e.g., (‘apple’, (1, 2, 3), [‘a’, ‘b’, ‘c’])
input_string = input(“Enter a nested tuple: “)
# Convert the input string to a tuple
nested_tuple = eval(input_string)
# Check if the entered value is a tuple
ifisinstance(nested_tuple, tuple):
print(“Entered nested tuple:”, nested_tuple)
else:
print(“Invalid input. Please enter a valid nested tuple.”)
Explanation:
• The program allows users to enter a nested tuple as a string.
• It then evaluates the string and converts it into a tuple.
• If the entered value is indeed a tuple, it is printed along with a confirmation message.
• If the input is not a tuple, an error message indicating invalid input is displayed.
Output:
TASK 4: Tuple Operations
Code:
#Tuple operations
tuple1 = (1, 2, 3)
tuple2 = (‘a’, ‘b’, ‘c’)
concatenated_tuple = tuple1 + tuple2
print(“Tuple1 is:”,tuple1)
print (“Tuple2 is:”,tuple2)
print(“concatenated_tuple is:”, concatenated_tuple)
# Output: (1, 2, 3, ‘a’, ‘b’, ‘c’)
261
CITS : IT & ITES - Computer Software Application - Exercise 128