Python Tuples

python-tuples-ipcisco.com-2

In Python, there are different data types that we use to store data collections. The are commonly four data types for this purposes; list, tuple, set and dictionary. In this lesson, we will focus on one of these data types, Python Tuples.

 

A Python Tuple can be created with round brackets. We can also crate tuples with tuple() constructor.

 

Tuples seems like python lists but there are some common differences between python lists and tuples. For example we can change Python Lists, but we can not change Python Tuples. We will talk about these differences in python tuple vs list lesson.

 


You can check these Tuple Lessons:


 

 

In a tuple, we can use different members or same members like python lists.

 

Here, we will give different example to learn Python Tuples better. Let’s start!

 

Below, we will create a tuple with has three members:

 

tuple1 = ("John", "Elena", "Albert")
print(tuple1)

 

The output of this code will be like below:

 

("John", "Elena", "Albert")

You can practice below!

python-tuples-ipcisco.com-1

 

You can also watch the video of this lesson.

 

We can also create this tuple with tuple() constructor like below:

 

tuple1 = tuple(("John", "Elena", "Albert"))
print(tuple1)

 

Here, do not forget the double round brackets. This method seems a little unnecessary but it is one of the way to create Python Tuples.

 

('John', 'Elena', 'Albert')

 

python-tuples-ipcisco.com-2

 

Let’s do another examples with numbers.

 

tuple1 = (1, 2, 3)
print(tuple1)

 

The output of this Python Tuples example will be like below:

 

(1, 2, 3)

 

Like Python Lists, Python Tuples has indexes to access specific member of the tuple. This indexes starts from 0 as it is in python lists.

 

Index [0]             First member

Index [1]             Second member

Index [2]             Third member

Index [3]             Fourth member

And so on.

 

Let’s access the second member of the above tuples.

 

tuple1 = ("John", "Elena", "Albert")
print(tuple1[1])

 

The output will be:

 

Elena

 

python-tuple-index-ipcisco.com-3

 

tuple1 = (1, 2, 3)
print(tuple1[1])

 

The output will be:

 

2

 

We can also use negative numbers as index numbers. And if we use negative numbers, then we start to count from the end of the tuple in reverse order. The last item in the tuple has index -1.

 

tuple1 = (1, 2, 3)
print(tuple1[-1])

 

The output of this python tuple code will be:

 

3

 

python-tuple-index-ipcisco.com-4

 

We can also access a series of items in a tuple with the help of colon ( : ) operator.

 

tuple1 = (1, 2, 3, 4, 5, 6)
print(tuple1[2:4])

 

(3, 4)

 

python-tuple-index-ipcisco.com-5

 

Here, the left side of the colon ( : ) shows the starting index and right side of the colon ( : ) shows the ending index. But the item that resides in the enfding index is not included.

 

We will also give different example to this python tuple access in the releted lesson.

 

 

By the way, according to python, tuples are defined as objects with data type ‘tuple’. So, if you would like to see this, you can use the below python code:

 

tuple1 = (1,2,3,4,5)
print(type(tuple1))

 

The output of this python code will be like below as expected:

 

<class 'tuple'>

 

python-tuple-class-ipcisco.com-6

 

We can use some methods with Python Tuples. Let’s do some examples of these methods used with Python Tuples.

 

In the below example, we will count the number of one member in the tuple. In other words, we will count 10 in the complete tuple with python count method.

 

numbers = (10,15,24,10,29,34,45,10)
x = numbers.count(10)
print(x)

 

The output of this code will be:

 

3

 

python-tuple-count-method-ipcisco.com-7

 

Now, let’s use python len function to measure the length of a Python Tuple.

 

numbers = (10,15,24,10,29,34,45,10)
x =len(numbers)
print(x)

 

The output of this python code will be:

 

8

 

python-tuple-len-method-ipcisco.com-8

 

We can use tuples with also python loops. In the below example, we will use “for loop” to print each member of the tuple to each line.

 

cars = ("bmw", "audi", "volvo", "mercedes")
for x in cars:
  print(x)

 

The output of this tuple loop example will be like below:

 

bmw
audi
volvo
mercedes

 

As we have mentioned at the beginning of this lesson, we can not change tuples in python. For example we can not do this with a usage like python tuple append. But there is always a way to do something. So, here we can do a trick to do this sometimes. For example, we can change a tuple to a list with list function. We can add a new member to this list and then we can convert the list to a tuple again. Here, the tuple can not be changes again but with a trick we achieve to add a memebr to it.

 

cars = ("bmw", "audi", "volvo", "mercedes")
a = list(cars)
a.append("aston martin")
cars = tuple(a)
print(cars)

 

In the output of this code, you will see the new member:

 

('bmw', 'audi', 'volvo', 'mercedes', 'aston martin')

 

python-tuples-ipcisco.com-9

 

In this Python Tuples lesson, we have learned how to create Python Tuples basically. In the following lessons, we will focus to the details of tuples and we will give more examples on one of the data types of python.

 

Back to: Python Programming Course > Python Tuples

Leave a Reply

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

Python Programming Course

Collapse
Expand
Latest Blog Posts