Python Tuple Addition

python-tuple-addition-ipcisco-1-dinginyasam

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


 

python-tuple-addition-ipcisco-2

 

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

 

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

 

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)

 

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 or we can do python tuple addition. You will use this method in your python programs to combine one more tuples.

gokhan-kosem-instructor-ipcisco

Gokhan Kosem is a Network Engineer, Instructor and the Founder of IPCisco.com with 15+ years of experience in Cisco, Nokia, Huawei, Juniper, Linux, Service Provider Networks, Routing and Switching technologies.

He has worked on the backbone networks of major service providers and network vendors including Nortel, Alcatel-Lucent (Nokia) and has extensive hands-on experience with Cisco, Huawei, Juniper and Nokia networking technologies.

He has trained thousands of networking students worldwide through IPCisco.com, Udemy, books, labs, quizzes, and educational content across multiple social media platforms.

IPCisco.com | Best Route to Your Dreams

Back to: Python Programming Course > Python Tuples

Leave a Reply

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

ipcisco gold membership

Python Programming Course

Collapse
Expand