Software DevelopmentLearn the concept of Tuples in Python

Learn the concept of Tuples in Python

Python Tutorial – Advanced Topics

In this article, we are going to talk about Tuples. A Tuple is a collection of objects (elements).

Well, does this definition remind you with something?! Right!! It is the same definition we used when we introduced Lists. In fact, tuples and lists are very similar; both are collections of comma-separated objects, both are accessible via zero-based index, and both allow operations like slicing. While lists are enclosed within square brackets, tuples – on the other hand – use parentheses. But the main difference between the two very-similar structures is that tuples are immutable, while lists are not. Remember when we talked about Strings; they were also immutable. Immutable means that they can’t be changed, or updated. So, tuples could be considered as read-only lists.

Declaring and Initializing Tuples
Like other Python structures, Tuples are declared and initialized in the step. For example:
1
This declares and initializes a tuple whose name is digits which contains the numbers from 0 to 9.

Similarly, the following initialization statements:
2
Accessing an Individual Element in a Tuple
The same way used to access individual array elements also applies here. To read the second element of the countries tuple, use the following statement:
3
4
Slicing
To extract a portion of a tuple based on a range of indexes.
5
Are they Really Immutable?!
I claimed that tuples are immutable, could you prove the opposite?!
6
The Python interpreter generated an error when trying to update the value of an element in a tuple. It says clearly that tuples don’t support item assignment. Do you believe me now?!

Ultimate Java Development and Certification Course
Tuples Functions and Methods
Tuples have a number of useful functions and methods. In this section, we are going to investigate those methods with illustrative examples.

len Function
The len function returns the length (number of elements) of a tuple.
7
min and max Functions
The min and max functions return the minimum and maximum values in a tuple, respectively.
8
tuple Function
The tuple function converts an array into a tuple.
9
The index Method
The index method searches a tuple (or part of it) for a specific value, and if found it returns the index of the element that matches the value.
10
The count Method
The count method also searches a tuple for a value, but instead of returning the index, it returns the number of occurrences (matches) for this value.
11
* * * * * *

In this article, we have talked about tuples. Tuples are very similar to arrays in many things, they are initialized the same way, their elements are accessed also the same way, and even the same functions and method applies to both like len, min, max, index, and count. Two main differences distinguish tuples from lists: the first is the use of parentheses instead of square brackets, and the second is that tuples are read-only (immutable).

See you in the next article.

2 COMMENTS

  1. Hi,

    A misunderstanding:
    While lists are enclosed within square brackets, tuples – on the other hand – use parentheses –

    The tuple operator is the comma not the parenthesis.

    a = 1, //A tuple
    a = (1) // A number

  2. It’s great that you are encouraging Python programmers to use tuples, but in my opinion there are several things missing here. First off, parentheses really have nothing to do specifically with tuples. They are used to isolate tuples when they occur within a larger list, but otherwise are not needed.

    The other thing is that you didn’t mention anything about why a programmer might use a tuple, as opposed to a list. You emphasized that they are immutable, and so they are, but it seems like you could just use a list anywhere you could use a tuple.

    The rationale for a tuple is to have a collection of related items. In fact, Guido has said that it was only an accident that tuples turned out to be iterable and indexable like lists.

    Tuples correspond to structs or records in other languages. A major operation on tuples is unpacking. A contrived example:
    values = ‘apple’, ‘banana’, ‘mango’
    f1, f2, f3 = values

    A more realistic example (imagine that the list *characters* was read from a file):
    characters = [
    ‘Fred’, ‘Flintstone’,
    ‘Barney’, ‘Rubble’,
    ‘Bugs’, ‘Bunny’,
    ‘Donald’, ‘Duck’,
    ]

    for first_name, last_name in character: # for loops with more than one variable unpack tuples
    print first_name, last_name

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -