Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along.
Learn to Program using Python: Lesson 1, Getting Started
provides an overall description of this online programming course.
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different code fragments, without losing your place, while you are reading about them.
Previous lessons (see
Learn to Program using Python: Nested Tuples
) have illustrated
- How to create a tuple.
- How to access a tuple item using indexing.
- How to slice a tuple.
- How to nest tuples.
This lesson will teach you how to create empty tuples and tuples containing only one item.
- Tuples are normally written as a sequence of items contained in matching parentheses.
- A tuple is an immutable sequence.
- Items in a tuple are accessed using a numeric index.
- Tuples can be nested.
Listing 1 shows the beginning of a Python script that
- Creates an empty tuple.
- Creates a single-item tuple.
- Nests the two tuples along with some strings in a third tuple.
- Determines the length of each of the tuples.
- Displays all of the above
# File Tuple03.py
(Note that some of the text in the Listings was highlighted using boldface for emphasis.)
The remaining parts of this program are shown as code fragments in subsequent Listings. A listing of the entire program is shown in Listing 7 near the end of the lesson.
What is an empty tuple?
As you might have guessed from the name, an empty tuple is just a pair of empty parentheses as shown by the first boldface line in Listing 1.
Listing 2 shows the output produced by the code in Listing 1. The empty tuple is displayed simply as a pair of empty parentheses, and the length of the empty tuple is shown to be zero (0).
Create/print empty tuple
Are you surprised?
There are probably no surprises regarding an empty tuple. However, there may be some surprises in the code fragment shown in Listing 3. This fragment deals with a tuple containing only one element.
print “Create/print one-
The syntax for creating a tuple with only one element is rather ugly, but is required to avoid ambiguity. In particular, it is necessary to follow the single tuple item with a comma as shown in the first boldface line in Listing 3.
Why is this comma necessary?
Had I written that line simply as follows without the extra comma,
t2 = “a”
the result would have been to create a new variable named t2 whose contents would be the string “a”.
Not a tuple
This would not indicate a tuple at all. Thus, the extra comma is required to make a single-item tuple unique and to distinguish it from other possibilities.
Output for the single-item tuple
Listing 4 shows the output produced by the code in Listing 3. The single-item tuple is shown in the first boldface line. As is always the case, the tuple is displayed in parentheses.
Create/print one-element tuple
What is the length of the tuple?
There is no surprise here. The length of the tuple as shown in Listing 4 is one (1) item.
Just to give you a little more practice in dealing with nested tuples, the code in Listing 5 nests the two tuples created above into a new tuple and stores the new tuple in the variable named t3.
print “Create/print nested
However unlike previous sample programs, in this case, literal parentheses are used to cause the tuple named t2 to be doubly nested.
In particular, as shown by the first boldface portion of code in Listing 5, the tuple named t2 and the string “Z” are used to create a tuple, which in turn, is nested in the tuple assigned to the variable named t3.
What does a doubly-nested tuple look like?
The double nesting is evidenced by the extra parentheses in the boldface portion of the output shown in Listing 6.
Create/print nested tuple
What is the length of this tuple?
The length of the tuple is also shown in Listing 6.
As you may have determined already, even though the tuple named t3 contains two nested tuples (one of which is doubly-nested), its overall length is only five (5) items.
Note that even though one of the tuples nested inside of t3 has a length of zero, it counts as one item when the length of t3, is determined.
# File Tuple03.py
- Unpack a tuple.
- Index inside of nested tuples.
- Slice nested tuples.
- Modify values stored in an object referred to by an item in a tuple.
Ans: False. A tuple is an ordered list of objects. This is evidenced by the fact that the objects can be accessed through the use of an ordinal index.
2. True or false? A tuple can only store references to other tuples.
Ans: False. A tuple can store references to any type of object.
3. True or false? All of the objects referred to by the items in a tuple must be of the same type.
Ans: False. The references stored as items in a tuple can refer to different types of objects.
4. True of false? A tuple is a mutable sequence.
Ans: False. A tuple is an immutable sequence.
5. True or false? The items in a tuple are accessed using a key, as in looking things up in a dictionary.
Ans: False. Items in a tuple are accessed using a numeric index that begins with the value zero (0).
6. Write a Python script that creates and displays an empty tuple.
Ans: See Listing 8.
# File Tuple13.py
7. Write a Python script that creates and displays a tuple having only one item.
Ans: See Listing 9.
# Copyright 2000, R. G. Baldwin
8. Write a Python script that creates and displays a tuple that is nested at least three levels deep.
Ans: See Listing 10.
# Copyright 2000, R. G. Baldwin
9. True or false? An empty tuple is created by placing a single comma inside of a pair of parentheses.
Ans: False. An empty tuple is just a pair of empty parentheses.
10. True or false? The len() method reports the length of an empty tuple as 1.
Ans: False. The len() method reports the length of an empty tuple as 0.
11. True or false? The syntax for a tuple with a single item is simply the element enclosed in a pair of matching parentheses as shown below:
t = (“a”)
Ans: False. The syntax for a tuple with a single item requires the item to be followed by a comma as shown below:
t = (“a”,)
12. True or false? The len() method reports the length of a tuple containing one item as 1.
13. What is the length of the tuple shown below?
((((‘a’, 1), ‘b’, ‘c’), ‘d’, 2), ‘e’, 3)
Ans: The length of this tuple is 3. See Listing 11 for confirmation.
# File Tuple15.py
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.