Section 6.17. Tuple Operators and Built-in Functions


6.17. Tuple Operators and Built-in Functions

6.17.1. Standard and Sequence Type Operators and Built-in Functions

Object and sequence operators and built-in functions act the exact same way toward tuples as they do with lists. You can still take slices of tuples, concatenate and make multiple copies of tuples, validate membership, and compare tuples.

Creation, Repetition, Concatenation

>>> t = (['xyz', 123], 23, -103.4) >>> t (['xyz', 123], 23, -103.4) >>> t * 2 (['xyz', 123], 23, -103.4, ['xyz', 123], 23, -103.4) >>> t = t + ('free', 'easy') >>> t (['xyz', 123], 23, -103.4, 'free', 'easy')


Membership, Slicing

>>> 23 in t True >>> 123 in t False >>> t[0][1] 123 >>> t[1:] (23, -103.4, 'free', 'easy')


Built-in Functions

>>> str(t) (['xyz', 123], 23, -103.4, 'free', 'easy') >>> len(t) 5 >>> max(t) 'free' >>> min(t) -103.4 >>> cmp(t, (['xyz', 123], 23, -103.4, 'free', 'easy')) 0 >>> list(t) [['xyz', 123], 23, -103.4, 'free', 'easy']


Operators

>>> (4, 2) < (3, 5) False >>> (2, 4) < (3, -1) True >>> (2, 4) == (3, -1) False >>> (2, 4) == (2, 4) True


6.17.2. Tuple Type Operators and Built-in Functions and Methods

Like lists, tuples have no operators or built-in functions for themselves. All of the list methods described in the previous section were related to a list object's mutability, i.e., sorting, replacing, appending, etc. Since tuples are immutable, those methods are rendered superfluous, thus unimplemented.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net