Home arrow Python arrow Page 4 - Data Types in Python

Tuples - Python

In this second of nine parts focusing on a quick overview of the Python language for experienced programmers, you'll learn how Python handles data types such as strings, and more. This article is excerpted from chapter four of Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright 2007 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. Data Types in Python
  2. Complex numbers
  3. Strings
  4. Tuples
By: O'Reilly Media
Rating: starstarstarstarstar / 4
September 18, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

A tuple is an immutable ordered sequence of items. The items of a tuple are arbitrary objects and may be of different types. To specify a tuple, use a series of expressions (the items of the tuple) separated by commas (,). You may optionally place a redundant comma after the last item. You may group tuple items within parentheses, but the parentheses are necessary only where the commas would otherwise have another meaning (e.g., in function calls), or to denote empty or nested tuples. A tuple with exactly two items is often known as a pair. To create a tuple of one item (often known as a singleton), add a comma to the end of the expression. To denote an empty tuple, use an empty pair of parentheses. Here are some tuples, all enclosed in the optional parentheses:

  (100, 200, 300)          # Tuple with three items
  (3.14,)                  # Tuple with one item
  ()                       # Empty tuple (parentheses NOT optional!)

You can also call the built-in type tuple to create a tuple. For example:

  tuple('wow')

This builds a tuple equal to:

  ('w', 'o', 'w')

tuple() without arguments creates and returns an empty tuple. When x is iterable, tuple(x) returns a tuple whose items are the same as the items in x .

Lists

A list is a mutable ordered sequence of items. The items of a list are arbitrary objects and may be of different types. To specify a list, use a series of expressions (the items of the list) separated by commas (,) and within brackets ([]). You may optionally place a redundant comma after the last item. To denote an empty list, use an empty pair of brackets. Here are some example lists:

  [42, 3.14, 'hello']      # List with three items
  [100]                    # List with one item
  []                       # Empty list

You can also call the built-in type list to create a list. For example:

  list('wow')

This builds a list equal to:

  ['w', 'o', 'w']

list() without arguments creates and returns an empty list. When x  is iterable, list(x) creates and returns a new list whose items are the same as the items in x. You can also build lists with list comprehensions, as discussed in "List comprehensions" on page 67.

Sets

Python 2.4 introduces two built-in set types, set and frozenset, to represent arbitrarily ordered collections of unique items. These types are equivalent to classes Set and ImmutableSet found in standard library module sets, which also exists in Python 2.3. To ensure that your module uses the best available sets, in any release of Python from 2.3 onwards, place the following code at the start of your module:

  try:
   
set
  except NameError:
    from sets import Set as set, ImmutableSet as frozenset

Items in a set may be of different types, but they must be hashable (see hash on page 162). Instances of type set are mutable, and therefore not hashable; instances of type frozenset are immutable and hashable. So you can't have a set whose items are sets, but you can have a set (or frozenset) whose items are frozensets. Sets and frozensets are not ordered.

To create a set, call the built-in type set with no argument (this means an empty set) or one argument that is iterable (this means a set whose items are the items of the iterable).

Please check back next week for the continuation of this article.



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