Tuples are immutable data sets made up of data of different types. While tuples are very similar to lists, they are different in those key ways.
Tuples cannot be changed once they are created. The data they hold can be passed to a different tuple, but the original tuple cannot be changed. This means that tuples don’t have methods for
manipulating them like lists do because they cannot be manipulated.
Like lists, tuples can also contain data of different types. Tuples can contain strings, integers, floats, booleans, and
even lists. Because tuples are immutable, they aren’t meant to be manipulated, so data types don’t matter nearly as much.
So, what are tuples even good for then. The answer is mostly storage. Tuples are great for storing sets of information that doesn’t need to or isn’t supposed to change. Projects like the Django web
framework use tuples to store settings, for example.
Creating tuples is a lot like creating a list with values already in it. Creating an empty tuple would be pointless, since they can’t be changed. Also, unlike lists, tuples use regular parenthesis
to enclose their data.
file_directories = ('/home/user/Pictures', '/var/www/siteroot/uploads', '/var/www/siteroot/staticfiles',)
The above example is similar to a configuration line that you may see in a program like Django. A set of file directories is not something that you’d want the program to be able to change, so they
are hard coded into a configuration file by the user. The user is still able to change them, but the program isn’t.
There’s something else to not there. There is a comma trailing after the last entry. In tuples, every element must be followed by a comma, regardless of its position.
Again, a tuple doesn’t have to store all of the same thing. The example below is a nonsensical demonstration of that, but is still valid Python.
random_junk = ('Bacon', 7, True, 11, 'Your mother was a hamster!',)
Tuples mostly just behave like limited lists. They can do many of the same basic things, but lack much of the more advanced functionality that modifies the contents of the list.
Navigating a tuple is just like navigating a list. Try accessing a couple of elements from that junk tuple.
random_junk = ('Bacon', 7, True, 11, 'Your mother was a hamster!',) print(random_junk)
Indexes are more predictable in tuples because the values don’t change. Even still, you can still navigate backward through a tuple.
random_junk = ('Bacon', 7, True, 11, 'Your mother was a hamster!',) print(random_junk[-2])
Length of Tuples
len() method works on tuples too. It works exactly the way it does with both strings and lists. Just put the list in the parenthesis.
random_junk = ('Bacon', 7, True, 11, 'Your mother was a hamster!',) print(len(random_junk))
Also, like with lists, you can use the
len() to help navigate through a tuple.
random_junk = ('Bacon', 7, True, 11, 'Your mother was a hamster!',) print(random_junk[len(random_junk) - 1])
Tuples are a sequential structure designed for storage and categorization. They aren’t meant to be manipulated like lists are, since they can’t be changed after they are created. They can, however,
store different data types that can be easily retrieved. If you are looking to implement a set of data in a program that should not be changed and could stand to be protected from change from the
program, a tuple is the right way to go.
Table of Contents
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions