In Python Programming, 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 tuple addition better. With these examples, you will learn this lesson better. So, let’s start!
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)
You can also check Python Tuples versus Python List
Let’s do another example with a string tuple. Here, we will combine two different tuples and create another tuple from the addition of these tuples.
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. To do this, we will use the below python code.
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 or we can do python tuple addition. You will use this method in your python programs to combine one more tuples.
Leave a Reply