Join Barron Stone for an in-depth discussion in this video Tuples, part of Programming Foundations: Real-World Examples.
- Lists can easily be changed by adding, removing, or modifying objects. There is another data type called the tuple which is similar to a list except for one key difference. A tuple is immutable which means it cannot be changed. Just like a list, a tuple contains a sequence of objects in order but once you've created a tuple, you can't change anything about it. So why would you want to use this tuple thing which is basically just a more restrictive version of a list? The short answer is simplicity. If I need to store or pass some nugget of information that's never going to change, then a tuple is a handy container to do that.
For example, when I was driving to the store earlier, I used the GPS function on my cellphone to help me navigate. When I use the GPS to find out where I am, it returns the answer as two values, a latitude and a longitude. Those two values always go together in that order so it makes sense for the GPS function to return them to me as a tuple. There's no reason I would need to change those latitude and longitude values later because they represent my current location as reported by the GPS. If I drive somewhere else and ask the GPS for my location again, it'll give me a new tuple corresponding to my new location.
The syntax for creating a tuple in Python is very similar to the way you create a list except that instead of using square brackets, you enclose the items in curved parentheses. I'll create a tuple now called my_tuple and give it a mixture of letters and numbers, 'a', 'b', 'c', 1,2, 3 If I look at the contents in my tuple, you'll see that the items maintain their relative order just like in a list. And just like with a list, I can index individual elements within the tuple to read their value.
If I access the element at index number two, which is the third position in my tuple, that returns the string object with the letter c. So far the tuple is working just like a list. However, if I try to replace the letter at index two in my tuple with another letter, let's make it a d, Python throws an error telling me that the 'tuple' object does not support item assignment. So, we can't replace items within a tuple. Also, I don't have access to the functions that insert and remove objects that I could use with lists.
Once the tuple has been created, it shall forever stay that way. Those are the basics for creating and accessing items in a tuple. Now, I want to show you an example of where you might encounter tuples in the wild. Since a Python function can only return a single object, if a function needs to return multiple values, it'll commonly pack them together into a tuple. Earlier I mentioned latitude and longitude as an example of two values that would always come packaged together. Python doesn't have a built in GPS capability to give me my latitude and longitude but it does have the ability to report the x and y coordinates of my mouse location on the screen, which is kind of the same thing.
So to demonstrate that, I'm going to open up the script called start_05_03_coordinates from the exercise files for this chapter. This is a simple example I created which gets the current x and y coordinates of the mouse when you click the mouse button. It uses the tkinter module to create a window, detect the mouse click, and get the coordinates. I'm not going to cover the tkinter part of the code in this video. All you need to know is that whenever you click your mouse in the window, it'll execute this mouse_click function which receives the x and y mouse coordinates as a tuple from this winfo_pointerxy function.
I named the tuple object that it returns coords and then print out the tuple as a whole and also print out the first and second elements of the tuple individually which are the x and y coordinates. When I run this script, it creates this small window that says tk in the title bar and when I click in the window, it'll retrieve my mouse coordinates and execute the print statements. You can see that the coords object is just a two element tuple containing the x and y values. If I click in the window again, then the function will generate a new tuple for the current mouse location and I print out those new values.
It makes sense for the pointer x y function to return the coordinates as an immutable tuple because it's reporting a piece of information After it gives me that tuple object, there's no reason why I would want or need to modify those values within the tuple. I'm only going to be reading the coordinates from that tuple and then using that information to do other things within my program.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming