Tuples are a lot like lists, and that's why we can define them in a pretty similar way as we did to define the lists. Simply put, a tuple is a sequence of data.
What makes them different from lists is that tuples are immutable, i.e., the data inside the tuples can't be modified, which is opposite in the case of lists. Other than this, tuples are very similar to lists and that would make it much easier for us to understand as we already know about lists. If you don't, we suggest you to first go through the List tutorial.
To define a tuple, we just have to assign a single variable with multiple values separated by commas, and that variable will be known as a Tuple.
>>> myTuple = 1, 2, 3, 4
If you try to print it in IDLE,
>>> print myTuple
(1, 2, 3, 4)
You can see in the above example, that
myTuple variable is actually a collection of integers 1, 2, 3 and 4. Also, note those circular brackets which appears while printing, around the integers, these will actually help you to distinguish between lists and tuples. Because in case of lists, we have square brackets around the list elements.
You can obviously add data of different types in a single tuple,
>>> secondTuple = 1, 2, "python", 4 >>> print secondTuple
(1, 2, "python", 4)
An empty tuple can be created using the
tuple() function or by just using an empty bracket
>>> emptyTuple = () >>> anotherEmptyTuple = tuple()
The above statements will create tuples with no elements in it. And the compiler would know that
anotherTuple are tuples, with no elements in them.
Indexing in tuples is also pretty similar to that in lists, the first element has index zero, and it keeps on increasing for the next consecutive elements. Also, backward indexing is also valid in tuples, i.e., the last element can be accessed using the index
-1 and the consecutive previous numbers by
-3 and so on. Let's take an example,
>>> example = "apple", "orange", "banana", "berry", "mango" >>> example
In the table below we have marked the tuple elements for both forward and backward indexing:
|Value||Forward Indexing||Backward Indexing|
As we know, that tuples are immutable, hence the data stored in a tuple cannot be edited, but it's definitely possible to add more data to a tuple. This can be done using the addition operator. Suppose there is a tuple,
>>> t = (1, 2, 3, 4, 5)
In case you want to add another element,
7 to the tuple, then you can do it as follows:
>>> t = t + (7,)
As you can see, we used the addition operator to
add(7,) to the tuple
>>> print t
(1, 2, 3, 4, 5, 7)
Hence, we can add any kind of element to a tuple, using the
If we try to think, what else can we do with the
+ operator, we might realize that it can be used to combine two tuples as well. For example:
>>> print (1, 2, 5, 8) + (2, 9, 4)
(1, 2, 5, 8, 2, 9, 4)
You can use a tuple(s) to create another tuple.
In order to delete a tuple, the
del keyword is used. In order to delete a tuple named
myTuple(which we defined earlier), follow the example below:
>>> del myTuple
myTuple will be deleted from the memory.
Slicing in tuples, works exactly the same like in the case of lists. Let's start with an example:
>>> t = (1, 2, 3, 4) >>> t[2:4]
t[2:4] means, slice the tuple starting from index
2 upto the index
4, and take that slice out.
Slicing can be done backwards as well using the negative indexes for traversing the tuple from backward direction.
The various operations that we can perform on tuples are very similar to lists. In fact, you just saw the
+ operator with tuples, it works with tuples just like it works with a list. Some other operators for tuples include:
Multiplying a tuple by any integer, x will simply create another tuple with all the elements from the first tuple being repeated x number of times. For example,
t*3 means, elements of tuple
t will be repeated
>>> t = (2, 5) >>> print t*3
(2, 5, 2, 5, 2, 5)
Using the addition operator, with two or more tuples, adds up all the elements into a new tuple. For example,
>>> t = (2, 5, 0) + (1, 3) + (4,) >>> print t
(2, 5, 0, 1, 3, 4)
in keyword, can not only be used with tuples, but also with strings and lists too. It is used to check, if any element is present in the sequence or not. It returns
True if the element is found, otherwise
False. For example,
>>> t = (1, 2, 3, 6, 7, 8) >>> 2 in t >>> 5 in t
As you might have already guessed, this function is used to get the number of elements inside any tuple.
>>> t = 1, 2, 3 >>> print len(t)
This is used to compare two tuples. It will return either
-1, depending upon whether the two tuples being compared are similar or not.
cmp() function takes two tuples as arguments, where both of them are compared. If T1 is the first tuple and T2 is the second tuple, then:
To find the maximum value in a tuple, we can use the
max() function, while for finding the minimum value,
min() function can be used.
>>> t = (1, 4, 2, 7, 3, 9) >>> print max(t) >>> print min(t)