The course is part of this learning path
Course Overview: We start with an introduction to an Interactive Development Environment and to the basic concepts of programming with the Python language. We then introduce how data is stored and represented within computers, and how primitive data types are used by Python to represent data within programs. We then learn more about how data is stored and collected by Python, how Strings and Tuples are stored and managed in Python and what the common operators are for manipulating these data types. Finally we learn how to create a simple function that outputs a string to the computer console.
- [Instructor] Okay, welcome back. In this lecture, I'm going to introduce you to the Python data type, Tuple. Tuples are a handy general data type used to store and maintain a sequence of immutable Python objects. The objective of this course is to introduce you to Tuples and to demonstrate what can be done with Tuples. I recommend that you complete the Halliwell lesson so you know how to troubleshoot Python programs. And additionally, complete the lessons on data types so you know the differences between an integer, character, and a Boolean. It will also help to know the differences between numbers and strings, and, finally to know how to assign data to variables.
Okay, let's get started. A Tuple data type is an ordered collection or sequence of values. Tuples can contain zero, one, or many items, each separated by a comma. They are immutable, meaning you cannot change the contents once you have created them. Let's not see what a Tuple looks like when we code with them. A typical Tuple contains multiple values of different data types where each item in the Tuple is comma separated. So for example, a Tuple could be established by assigning the values one comma two comma three comma four. Notice here that this is not a list since lists are defined within square brackets, and we haven't used square brackets here. When we view t, which is in this case, the variable name for this Tuple, you'll see that it's now demarcated by rounded brackets, denoting the fact that t has been created as a Tuple data type. In this example, a Tuple is still indexed in the same manner as a list where, for example, you can retrieve the first item within a Tuple by using the index zero. Indexing Tuples always starts at zero and increases as you move forward in the Tuple. Now, it's important to understand and recognize that Tuples are immutable. If you attempt to change or reassign any items within an existing Tuple, an exception like this will be raised since Tuples are immutable. Next, we consider the recursive aspect of Tuples where you can define a new Tuple by referring to an existing Tuple like this. Here you can say that new Tuple we just created now contains the previous Tuple. Next, I'll show you how an empty Tuple can be created and this is performed by initializing it first with empty rounded brackets. In here, you can see that the resulting Tuple is indeed empty. If we next attempt to create a Tuple in the following manner, highlighting here that it has been initialized without the use of any commas, then this just becomes a normal string by definition. Now, we can perform many simple operations on Tuples. For example, we can examine the length of any Tuple. In this case, we can see that the expected length of the empty Tuple is indeed zero, and that the length of the u Tuple is two, and the length of the t2 variable which is a string is five. Now, let's determine a new Tuple named t3 and this time, assign it the string hello. But this time, we will append a comma to it like this. Now, because we added a comma after the string, we have forced it to become a Tuple. The length of t3 is one. With the inclusion of a comma, it's now been recognized by the Python interpreter as a Tuple and has a length of one. Now, this is extremely useful when we next explore the concept called functions, and how functions return data types and Tuples are extremely powerful because it allows you to return multiple data types or values from the same function. Okay, that concludes this introduction to Tuples. You now understand what, when, and how Tuples are used. You now understand the concept of what a Tuple is and how to manipulate Tuples. And importantly, you understand and recognize that Tuples are immutable.
About the Author
Andrew is an AWS certified professional who is passionate about helping others learn how to use and gain benefit from AWS technologies. Andrew has worked for AWS and for AWS technology partners Ooyala and Adobe. His favorite Amazon leadership principle is "Customer Obsession" as everything AWS starts with the customer. Passions around work are cycling and surfing, and having a laugh about the lessons learnt trying to launch two daughters and a few start ups.