Joining Tuples
You can join two or more tuples using the +
operator to create a new tuple. Tuples can also be multiplied using the *
operator.
Concatenating Tuples
Example 1: Joining Tuples
# Joining tuples
tuple1 = ("Apple", "Banana")
tuple2 = ("Cherry", "Date")
joined_tuple = tuple1 + tuple2
print("Joined Tuple:", joined_tuple)
Output
Joined Tuple: ('Apple', 'Banana', 'Cherry', 'Date')
Multiplying Tuples
Example 2: Repeating Tuples
# Multiplying tuples
tuple_data = ("Python",)
multiplied_tuple = tuple_data * 4
print("Multiplied Tuple:", multiplied_tuple)
Output
Multiplied Tuple: ('Python', 'Python', 'Python', 'Python')
Using sum()
Function with Tuples
Example 3: Summing Numeric Tuples
# Summing tuples with numbers
tuple_numbers = (1, 2, 3, 4, 5)
total = sum(tuple_numbers)
print("Sum of Tuple Elements:", total)
Output
Sum of Tuple Elements: 15
Explanation: The sum()
function can be used to sum up numeric elements in a tuple.