Home Python Page 3 - Tuples and Other Python Object Types

# Other Core Types - Python

In this conclusion to a four-part article series on Python object types, we will finish our discussion of dictionaries, move on to tuples, and cover related material. This article is excerpted from chapter four of the book Learning Python, Third Edition, written by Mark Lutz (O'Reilly, 2008; ISBN: 0596513984). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Rating:  / 4
February 05, 2009

SEARCH DEV SHED

TOOLS YOU CAN USE

Beyond the core types we’ve seen so far, there are others that may or may not qualify for membership, depending on how broad the category is defined to be. Sets, for example, are a recent addition to the language. Sets are containers of other objects created by calling the built-in set function, and they support the usual mathematical set operations:

>>> X = set('spam')

>>> Y = set(['h',
'a', 'm'])
# Make 2 sets out of sequences

>>> X, Y

(set(['a', 'p', 's', 'm']), set(['a', 'h', 'm']))

>>> X & Y                       # Intersection
set(['a', 'm'])

>>> X | Y                       # Union
set(['a', 'p', 's', 'h', 'm'])

>>> X – Y                       # Difference
set(['p', 's'])

In addition, Python recently added decimal numbers (fixed-precision floating-point numbers) and Booleans (with predefinedTrueandFalseobjects that are essentially just the integers 1 and 0 with custom display logic), and it has long supported a special placeholder object calledNone:

>>> import decimal              # Decimals
>>> d =
decimal.Decimal('3.141')

>>> d + 1

Decimal("4.141")

>>> 1 > 2, 1 < 2                # Booleans
(False, True)
>>> bool('spam')

True

>>> X = None                  # None placeholder
>>> print X

None
>>> L = [None] * 100          # Initialize a list of 100 Nones

>>> L

[None, None, None, None, None, None, None, None, None, None, None, None, None,
...a list of 100 Nones...
]

>>> type(L)                     # Types
<type 'list'>
>>> type(type(L))              # Even types are objects

<type 'type'>

 >>> More Python Articles          >>> More By O'Reilly Media