Home arrow Python arrow 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.

TABLE OF CONTENTS:
  1. Tuples and Other Python Object Types
  2. Tuples
  3. Other Core Types
  4. How to Break Your Code’s Flexibility
  5. And Everything Else
By: O'Reilly Media
Rating: starstarstarstarstar / 4
February 05, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: