Python Tuple Addition

python-tuple-addition-ipcisco-1-dinginyasam

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)

 

python-tuple-addition-ipcisco-2

 

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')

 

how-to-add-python-tuples-together-1

 

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)

 

python-tuple-addition-ipcisco-1-dinginyasam

 

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.

 

Back to: Python Programming Course > Python Tuples

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k