#Concatenating two Tuples:
tup1 = (1, 2, 7,3, 4, 5)
tup2 = (3, 4, 5, 6, 7)
# concatenate tuples using + operator
tup3 = tup1 + tup2
print(tup3)
output:
(1, 2, 7, 3, 4, 5, 3, 4, 5, 6, 7)
sum() function
We can also use the Python built-in function sum to concatenate two tuples.
tup1 = (1, 2, 7,3, 4, 5)
tup2 = (3, 4, 5, 6, 7)
tup3=sum((tup1,tup2),())
print(tup3)
#(1, 2, 7, 3, 4, 5, 3, 4, 5, 6, 7)
#nested uples
nestedtuple = ((20, 40, 60), (10, 30, 50), "Python")
print(nestedtuple[2][0])
for i in nestedtuple:
print("tuple", i, "elements")
for j in i:
print(j, end=", ")
print("\n")
output:
P
tuple (20, 40, 60) elements
20, 40, 60,
tuple (10, 30, 50) elements
10, 30, 50,
tuple Python elements
P, y, t, h, o, n,
#built-in functions with tuple min() and max():
tup1 = ('xyz', 'zara', 'abc')
print(max(tup1))
print(min(tup1))
tup2 = (11, 22, 10, 4)
print(max(tup2))
print(min(tup2))
output:
zara
abc
22
4
Comments
Post a Comment