Tuples Datatypes In Python

in STEMGeeks2 months ago

Welcome back to our next post on getting started coding with the Python programming language. We've been improving the functionality of our code in the past few articles but in this and the next few articles we are going to do a little more explanation on some of the more advanced data types available in Python.

In this article as you might have guessed by the title we are going to take a quick deep dive into using Tuples in Python. We haven't looked at using Tuples in our code before, but simply put a Tuple is a datatype in Python used to store multiple items in a single variable. Although we've look at using Lists previously, Tuples are different as they are ordered and the values in the Tuple cannot be changed once they are created but duplicate values are allowed. This basically means that we cannot change, add or remove items after the tuple has been created but Tuples can also hold any data types and they can be mixed.

To demonstrate how to create and work with Tuples, open your interactive Python shell and we will perform some basic steps to show you.

Creating Tuples is simple. As you can see in the example below, we use the equal sign to create the Tuple values inside round brackets and assign these values to the Tuple s name on the left. Using the Tuple name to then display the values stored inside as we have below with the print() function:

>>> new_fruit_tuple = ("apple", "banana", "cherry")
>>> print(new_fruit_tuple)
('apple', 'banana', 'cherry')

Just like using Lists, the values inside the Tuple are indexed, so if we need to view specific items in the Tuple we can use the index number as we below:

>>> new_fruit_tuple[1]
'banana'

The len() function allows you to print the length of the Tuple. This could then be used to work through all the values in the Tuple or something similar:

>>> len(new_fruit_tuple)
3

If you create a Tuple with only one item in it, Python will not recognize as a Tuple unless you place a comma after the first item.

>>> not_a_tuple = ("test")
>>> type(not_a_tuple)
<class 'str'>
>>> a_tuple = ("test",)
>>> type(a_tuple)
<class 'tuple'>

To test if a specific value is in your Tuple, you can use a simple if statement like we have below:

>>> new_fruit_tuple = ("apple", "banana", "cherry")
>>> if "apple" in new_fruit_tuple:
...   print("Yes, apple is in the tuple")
...
Yes, apple is in the tuple

NOTE: If you ever need to make a change to your Tuple like adding or removing items from it, there is a workaround. You can convert your Tuple into a List, make your specific changes and then convert it back into the Tuple you originally needed.

So What's The Point Of Using Tuples?


You may be wondering why we even need to worry about Tuples when we have something like Lists available to use in our code. There are probably two main reasons why you would want to use Tuples over Lists:

  • The first is that Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate over those values, using a Tuple instead of a List will be a better option.
  • The second would be that using a Tuple makes your code safer as if you “write-protect” data that does not need to be changed. By using a Tuple instead of a List implies that your data is constant and cannot be changed without using a special function.

Ok, we've been pretty quick with this weeks article but hopefully it gave you a good and quick overview on and some of the ways you can be using them in your code.

Found this post useful? Kindly tap the up vote button below! :)

About The Author

I am a DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, and development(Python).

Posted with STEMGeeks

Sort:  

Tuples are useful to return multiple values from a function. There's clever stuff you can do with unpacking them, e.g.

c = (1,2,3)
x,y,z = c

I love that you can loop through all sorts of things in Python.

!LUV

Hi @run.vince.run, you were just shared some LUV thanks to @steevc. Holding 10 LUV in your wallet enables you to give up to 3 LUV per day, for free. See the LUV in your wallet at https://hive-engine.com or learn about LUV at https://peakd.com/@luvshares