Uploaded by Michael Jin [Student]

CIND830 Module 6 - Tuples transcript

advertisement
Speaker 1:
Tuples. In this section, we will cover the second data structure that we have in this module. Tuples are
pretty much similar to lists, as they have the ability to store different types of data elements in
sequential order as well. However, they are immutable, much faster, and take small size in memory in
comparison to lists. Similar to what we covered when we talked about the lists, we will explore tuples
structure and basic operations. Then an illustrative example will be given on the implementation of
those operations.
Tuples structure. Unlike lists, and similar to strings, tuples are immutable. A tuple resembles a
list, as it's a sequence of elements. However, such sequence is enclosed in parentheses rather than
square brackets. Most of the operators and methods used with lists can be used in a similar fashion with
tuples. A tuple is preferred to be used if the structure of the object will not change. For example, an
object that represents the set of vowels in a text processing application.
Tuples operations. Tuples implement all of the common sequence operations. The in and not in
operations can be used for containment checking. Concatenating tuples using the + operation always
results in a new tuple. Using the * operation or the multiplication operation will result in adding the
tuple to itself a fixed number of times, rather than multiplying it by a number. The len, min, and max
functions can be used to acquire the length, smallest item, and largest item in a tuple. The subscript
operator along with the slice notation can be used to extract certain items from a tuple.
This example begins with highlighting one of the differences between lists, strings, and tuples,
which is the ability to group different or mixed types of data elements. You'll probably notice that it's
only the string data structure that cannot store mixed types of data elements. The program continues to
show some of the basic operations that we covered in the previous slides, starting with checking the
occurrence of a specific data element in a tuple and showing how to group the elements of two tuples
to form another tuple. Then, how to use the multiplication symbol to repeat the data elements of a
tuple a specific number of times. Finally, showing how to extract data elements from a tuple using either
the subscript operator or the slicing notation.
Program execution. During the program execution, the interpreter will create three different
data structures, iList, iString, and iTuple. Look at how the interpreter creates those objects. The list and
the tuple objects are pretty much similar to each other. Each has a string and an integer combined
together. However, the string has only one string literal.
On line number 5, the interpreter will check whether the string "Python" is one of the tuple
object's data elements. Since this is correct, the interpreter will display the Boolean object True on the
screen. On line number 7, a new tuple will be created, and this tuple includes two existing tuples. Look
at the uTuple elements. You can see that there are four elements, which is the result of adding the items
of iTuple's structure to itself. On line 9, the interpreter will use the asterisk symbol to repeat the iTuple
structure's elements three times, resulting in a new tuple with six elements. The output of line 11 is the
data element at index number 2, or the third element of the wTuple structure, the string "Python."
Finally, the interpreter will use the slicing notation as indicated on line number 13 to extract the
elements at index 2, 3, and 4 of the wTuple structure, which are "Python," 830, and "Python."
CIND830 Module 6 - Video 2 - Tuples
Page 1 of 1
Download