CBSE Class 12 Computer Science Question 28 of 57

Review of Python Basics — Question 28

Back to all questions
28
Question

Question 23

Write a program to input any values for two tuples. Print it, interchange it and then compare them.

Solution
tuple1 = eval(input("Enter the first tuple: "))
tuple2 = eval(input("Enter the second tuple: "))

print("Original Tuples:")
print("Tuple 1:", tuple1)
print("Tuple 2:", tuple2)

tuple1, tuple2 = tuple2, tuple1

print("\nSwapped Tuples:")
print("Tuple 1 (after swapping):", tuple1)
print("Tuple 2 (after swapping):", tuple2)

if tuple1 == tuple2:
    print("\nThe swapped tuples are equal.")
else:
    print("\nThe swapped tuples are not equal.")
Output
Enter the first tuple: (5, 6, 3, 8, 9)
Enter the second tuple: (77, 33, 55, 66, 11)
Original Tuples:
Tuple 1: (5, 6, 3, 8, 9)
Tuple 2: (77, 33, 55, 66, 11)

Swapped Tuples:
Tuple 1 (after swapping): (77, 33, 55, 66, 11)
Tuple 2 (after swapping): (5, 6, 3, 8, 9)

The swapped tuples are not equal.
Answer

tuple1
=
eval
(
input
(
"Enter the first tuple: "
))
tuple2
=
eval
(
input
(
"Enter the second tuple: "
))
print
(
"Original Tuples:"
)
print
(
"Tuple 1:"
,
tuple1
)
print
(
"Tuple 2:"
,
tuple2
)
tuple1
,
tuple2
=
tuple2
,
tuple1
print
(
"
\n
Swapped Tuples:"
)
print
(
"Tuple 1 (after swapping):"
,
tuple1
)
print
(
"Tuple 2 (after swapping):"
,
tuple2
)
if
tuple1
==
tuple2
:
print
(
"
\n
The swapped tuples are equal."
)
else
:
print
(
"
\n
The swapped tuples are not equal."
)
Output
Enter the first tuple: (5, 6, 3, 8, 9)
Enter the second tuple: (77, 33, 55, 66, 11)
Original Tuples:
Tuple 1: (5, 6, 3, 8, 9)
Tuple 2: (77, 33, 55, 66, 11)

Swapped Tuples:
Tuple 1 (after swapping): (77, 33, 55, 66, 11)
Tuple 2 (after swapping): (5, 6, 3, 8, 9)

The swapped tuples are not equal.