Monday, June 27, 2022
HomeSoftware DevelopmentCreate an Empty Tuple in Python

Create an Empty Tuple in Python


Python tutorials


 

This document is part of a series of Python programming tutorials and lessons designed to teach  developers how to program using the Python programming language.

Read: Top Online Courses to Learn Python

Introduction to Tuples in Python

This is the third in a series of Python programming tutorials designed to teach you about uples. The previous tutorials in this series illustrated how to:

  • How to create a tuple.
  • How to access a tuple item using indexing.
  • How to slice a tuple.
  • How to nest tuples.

You can read that article by visiting: Learn to Program using Python: Nested Tuples.

This Python tutorial, in turn, will teach programmers 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 Showing Tuples in Python

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

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

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”.

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

There is no surprise here.  The length of the tuple as shown in Listing 4 is one (1) item.

Read: How to Sort Lists in Python

Nested Tuples in Python

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 in Python

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.

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

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 Showing Tuples in Python

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

Read: How to Create and Print Lists in Python

What’s Next?

Upcoming Python tutorials 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 of How to Work with Python Tuples

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


Read more Python programming and software development tutorials.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments