In Python, we can combine tuples and we can do python tuple addition. Here, we can add two or more tuples and create a new tuple from these tuples. Let’s give an example and learn python tuple addition better.
numbertuple1 = (1,2,3,4,5) numbertuple2 = (10,20,30) numbertuple3 = (102,205,308) numbertuple = numbertuple1 + numbertuple2 + numbertuple3 print(numbertuple)
With this example, we combine three tuples into one tuple. The output of this python code will be like below:
(1, 2, 3, 4, 5, 10, 20, 30, 102, 205, 308)
Let’s do another example with a string tuple.
cars1 = ("BMW", "Mercedes", "Audi") cars2 = ("Aston Martin", "Porche", "Jaguar") cars = cars1 + cars2 print(cars)
The output of this python tuple addition code will be like below:
('BMW', 'Mercedes', 'Audi', 'Aston Martin', 'Porche', 'Jaguar')
We can also repeat the existing tuple items in another tuple.
numbers1 = (1,2,3,4,5) numbersx = numbers1 * 3 print(numbersx)
As you can see below, the new tuple will be created with the items ofm the first tuple three times.
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
Here, we have learned that, we can add two or more tuples together to create another tuple. In other words, we can combine tuples, we can do python tuple addition.
No related posts.