October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Learn to Program Using Python: Empty and Single-Item Tuples

  • May 18, 2004
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »


Preface

This document is part of a series of online tutorial lessons designed to teach you how to program using the Python scripting language.

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.

Viewing tip

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.

Introduction

This is the third in a series of lessons designed to teach you about tuples.

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.

What Is a Tuple?

A tuple is like a list whose values cannot be modified.  It is an ordered list of objects, and it can contain references to any type of object.
  • 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.

Sample Program

Empty and single-item tuples

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
#-------------------------------
print "Create/print empty \
tuple"
t1 = ()
print t1
print "Length of empty tuple is"
print len(t1)

Listing 1

(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
()
Length of empty tuple is
0

Listing 2

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-\
element tuple"
# Note the req trailing comma
t2 = "a",
print t2

print "Length of one-element \
tuple is:"
print len(t2)

Listing 3

Ugly syntax

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
('a',)
Length of one-element tuple is:
1

Listing 4

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.

Nested tuples

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 \
tuple"
t3 = "A",t1,"B",(t2,"Z"), "C"
print t3

print "Length of nested tuple \
is"
print len(t3)

Listing 5

Doubly-nested tuples

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
('A', (), 'B', (('a',), 'Z'), 'C')
Length of nested tuple is
5

Listing 6

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.

Listing of Sample Program

A complete listing of the program is shown in Listing 7.
 
# File Tuple03.py
# Rev 7/31/00
# Copyright 2000, R. G. Baldwin
# Illustrates empty tuples and
#  tuples with only one element
#
#-------------------------------
print "Create/print empty \
tuple"
t1 = ()
print t1
print "Length of empty tuple is"
print len(t1)

print "Create/print one-\
element tuple"
# Note the req trailing comma
t2 = "a",
print t2
print "Length of one-element \
tuple is:"
print len(t2)


print "Create/print nested \
tuple"
t3 = "A",t1,"B",(t2,"Z"), "C"
print t3

print "Length of nested tuple \
is"
print len(t3)

Listing 7

What's Next?

Upcoming lessons will show you how to:
  • 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.

Review

1.  True or false?  A tuple is an unordered list of objects.

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
# Rev 7/31/00
# Copyright 2000, R. G. Baldwin
# Illustrates empty tuples
#
#-------------------------------
print "Create/print empty \
tuple"
t1 = ()
print t1

Listing 8

7.  Write a Python script that creates and displays a tuple having only one item.

Ans:  See Listing 9.
 

# Copyright 2000, R. G. Baldwin
# Illustrates tuples with only
#  one element
#-------------------------------
print "Create/print one-\
element tuple"
# Note the req trailing comma
t2 = "a",
print t2

Listing 9

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
# Illustrates deeply-nested 
#  tuple
#-------------------------------
t1 = "a",
print t1
t2 = t1,"b","c"
print t2
t3 = t2,"d"
print t3
t4 = t3,"e"
print t4

Listing 10

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:  FalseThe 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.

Ans:  True.

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
# Rev 7/31/00
# Copyright 2000, R. G. Baldwin
# Illustrates length of deeply- 
#  nested tuple
#-------------------------------
t1 = "a",1
t2 = t1,"b","c"
t3 = t2,"d",2
t4 = t3,"e",3
print t4
print len(t4)

Listing 11



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.

Baldwin@DickBaldwin.com






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel