Python is a very powerful and flexible programming language. It also happens to be one of the best languages for beginners, because it’s not only so easy to learn, it doesn’t require complicated compilers, and, instead, uses an interpreter to run programs. So instead of writing a program, compiling it, and running it…you simply write it and run it.

For the most part, Python is quite simple to grasp. We’ve already covered reading text from files, the Range function, data types, and numerous other concepts surrounding this user-friendly language.

But not every concept is as easy to grasp. One such idea is the tuple. A tuple is a single variable that can hold multiple items. Although once you understand what this feature does, it’s rather simple to work with. Like Python sets, lists and dictionaries, tuples are built-in data types, but faster to work with than those other choices.

With that said, let’s learn what a tuple is and how to write/use them.

What Is a Tuple?

In simplest terms, a Python tuple is similar to a list, which is a simple way of defining multiple variables.

So instead of defining your variables like so:

You could then print out that list of variables like so:

That’s all fine and good, but with a list is much easier. To get the same effect, a list of colors would look like this:

You could then print out the list like so:

Using lists makes it much easier to efficiently define similar variables.

Read More:   Update Why You Shouldn’t Host Your Own GraphQL Endpoint

Similar to lists, a tuple is a collection of values that are declared using parentheses. So instead of this list:

We format tuples like this:

Beyond the obvious usage of [] vs (), what’s the difference between a tuple and a list? Simply put, you can change the elements of a list (once they are assigned), whereas with a tuple you cannot.

Want to see the difference from the perspective of the Python interpreter? Open a terminal window on a machine that has Python installed and access the console with the command:

python3

Type:

names_l = ['Olivia', 'Nathan', 'Bethany', 'Jacob']

Now, type:

names_t = ('Olivia', 'Nathan', 'Bethany', 'Jacob')

Okay, now type:

print(type(names_l))

Hit Enter on your keyboard and you should see:

<class 'list'>

Type:

print(type(names_t))

Hit Enter on your keyboard and you should see:

<class 'tuple'>

So far, the difference is minimal to the programmer. One of the keys to using tuples over lists is that lists cannot be used in dictionaries (because lists are mutable).

Hold up a minute. What is this dictionary you speak of? A Python dictionary is similar to a list, in that it is a collection of data. Dictionaries are Python’s implementation of associate arrays and use key-value pairs like so:

You can then print out the dictionary like so:

print (NBA_TEAM) 

Types of Tuples

There are four different types of tuples you can use. First, there’s the empty tuple, which is written like this:

empty_tuple = ()

Print out that tuple (with the command print(empty_tuple)) and you’ll see nothing but () as the output.

Read More:   Rethinking App Development and Delivery – InApps 2022

The next type of tuple is one with integers, which looks like this:

integer_tuple = (1, 2, 3)

Print out that tuple and you should see:

(1, 2, 3)

Next, we have a tuple with mixed data types, which might look like this:

mixed_tuple = (0, "Hello", 1.2, "World!")

Print out that tuple and you should see something like:

(0, 'Hello', 1.2, 'World!')

Finally, we have the nested tuple which is a tuple within a tuple and looks like this:

nested_tuple = ("aardvark", [0, 1, 2], (2, 1, 0))

As you might expect, the output of print(nested_tuple) will look something like:

('aardvark', [0, 1, 2], (2, 1, 0))

Accessing Tuple Elements

This is where tuples can get a bit tricky. Let’s create a basic tuple like so:

Now, issue the command:

print(tns_tuple[0])

What you should see printed out is:

N

Remember, in Python, counting starts at 0, so the N is at the 0 position in the tuple.

Where it gets a bit tricky is when you want to index a nested tuple. Let’s create the following nested tuple:

Let’s access various elements from within that nested tuple. Because this is nested, we’ve effectively created three tuples:

If I want to print out the element 3 in tuple 0 (which would be the letter “e” in “kubernetes”), I would issue the command:

print(n_tuple[0][3])

What I’ve done is tell the Python interpreter to print out the value for the third position in the first tuple (tuple 0).

We can also access a range of our items within a tuple by doing what’s called slicing. This is done by using the : character. Let’s go back to our basic tuple:

To print out the first three elements, we could do something like:

Read More:   Modern Observability Is a Single Braid of Data – InApps 2022

print(tns_tuple[0:3])

That command would print out:

('N', 'e', 'w')

To print out the last five elements, the command would be:

print(tns_tuple[3:8])

The above command would print out:

We can use concatenation and repetition with tuples. Concatenation is done with the + operator like so:

print(('T', 'h', 'e') + ('N', 'e', 'w') + ('S', 't', 'a', 'c', 'k') )

The output of that concatenated tuple looks like this:

We can use repetition with the * operator like so:

print(("InApps",) * 3)

The output of the above command would look like this:

Finally, another really handy feature with tuples is the ability to count and index items. Let’s go back to our basic tuple (adding a bit to it):

Let’s say you want to count how many k’s are in the tuple. This can be done like so:

print(tns_tuple.count('k'))

The output of the above command would be 2 because there are two k’s in the tuple. But what positions to those k’s hole? Let’s find out with:

print(tns_tuple.index('k'))

Ah, we’ve run into a problem. The above command will print out 7 and stop. It won’t continue indexing. That’s because tuple.index was designed to do just that.

In order to locate the second instance, we have to use a bit of trickery like so:

That should print out:

And there you have it…your introduction to Python tuples. Make sure to go back through the rest of the series and keep coming here to InApps for more Python goodness.