~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Doc/library/stdtypes.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
172
172
any operand is a complex number, the objects are of different types that cannot
173
173
be compared, or other cases where there is no defined ordering.
174
174
 
175
 
.. index:: 
 
175
.. index::
176
176
   single: __eq__() (instance method)
177
177
   single: __ne__() (instance method)
178
178
   single: __lt__() (instance method)
322
322
      module: math
323
323
      single: floor() (in module math)
324
324
      single: ceil() (in module math)
 
325
      single: trunc() (in module math)
325
326
      pair: numeric; conversions
326
327
      pair: C; language
327
328
 
330
331
   for well-defined conversions.
331
332
 
332
333
(4)
333
 
   float also accepts the strings "nan" and "inf" with an optional prefix "+" 
 
334
   float also accepts the strings "nan" and "inf" with an optional prefix "+"
334
335
   or "-" for Not a Number (NaN) and positive or negative infinity.
335
336
 
336
337
(5)
337
338
   Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
338
339
   programming languages.
339
340
 
340
 
   
 
341
 
341
342
 
342
343
All :class:`numbers.Real` types (:class:`int` and
343
344
:class:`float`) also include the following operations:
344
345
 
345
 
+--------------------+--------------------------------+--------+
346
 
| Operation          | Result                         | Notes  |
347
 
+====================+================================+========+
348
 
| ``trunc(x)``       | *x* truncated to Integral      |        |
349
 
+--------------------+--------------------------------+--------+
350
 
| ``round(x[, n])``  | *x* rounded to n digits,       |        |
351
 
|                    | rounding half to even. If n is |        |
352
 
|                    | omitted, it defaults to 0.     |        |
353
 
+--------------------+--------------------------------+--------+
354
 
| ``math.floor(x)``  | the greatest Integral <= *x*   |        |
355
 
+--------------------+--------------------------------+--------+
356
 
| ``math.ceil(x)``   | the least Integral >= *x*      |        |
357
 
+--------------------+--------------------------------+--------+
 
346
+--------------------+------------------------------------+--------+
 
347
| Operation          | Result                             | Notes  |
 
348
+====================+====================================+========+
 
349
| ``math.trunc(x)``  | *x* truncated to Integral          |        |
 
350
+--------------------+------------------------------------+--------+
 
351
| ``round(x[, n])``  | *x* rounded to n digits,           |        |
 
352
|                    | rounding half to even. If n is     |        |
 
353
|                    | omitted, it defaults to 0.         |        |
 
354
+--------------------+------------------------------------+--------+
 
355
| ``math.floor(x)``  | the greatest integral float <= *x* |        |
 
356
+--------------------+------------------------------------+--------+
 
357
| ``math.ceil(x)``   | the least integral float >= *x*    |        |
 
358
+--------------------+------------------------------------+--------+
358
359
 
359
360
For additional numeric operations see the :mod:`math` and :mod:`cmath`
360
361
modules.
419
420
   overflow check.
420
421
 
421
422
 
 
423
Additional Methods on Integer Types
 
424
-----------------------------------
 
425
 
 
426
.. method:: int.bit_length()
 
427
 
 
428
    Return the number of bits necessary to represent an integer in binary,
 
429
    excluding the sign and leading zeros::
 
430
 
 
431
        >>> n = -37
 
432
        >>> bin(n)
 
433
        '-0b100101'
 
434
        >>> n.bit_length()
 
435
        6
 
436
 
 
437
    More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
 
438
    unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
 
439
    Equivalently, when ``abs(x)`` is small enough to have a correctly
 
440
    rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
 
441
    If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
 
442
 
 
443
    Equivalent to::
 
444
 
 
445
        def bit_length(self):
 
446
            s = bin(x)          # binary representation:  bin(-37) --> '-0b100101'
 
447
            s = s.lstrip('-0b') # remove leading zeros and minus sign
 
448
            return len(s)       # len('100101') --> 6
 
449
 
 
450
    .. versionadded:: 3.1
 
451
 
 
452
 
422
453
Additional Methods on Float
423
454
---------------------------
424
455
 
430
461
    original float and with a positive denominator.  Raises
431
462
    :exc:`OverflowError` on infinities and a :exc:`ValueError` on
432
463
    NaNs.
433
 
    
434
 
    .. versionadded:: 2.6
435
464
 
436
465
Two methods support conversion to
437
466
and from hexadecimal strings.  Since Python's floats are stored
565
594
Sequence Types --- :class:`str`, :class:`bytes`, :class:`bytearray`, :class:`list`, :class:`tuple`, :class:`range`
566
595
==================================================================================================================
567
596
 
568
 
There are five sequence types: strings, byte sequences, byte arrays, lists,
569
 
tuples, and range objects.  (For other containers see the built-in
570
 
:class:`dict`, :class:`list`, :class:`set`, and :class:`tuple` classes, and the
571
 
:mod:`collections` module.)
 
597
There are six sequence types: strings, byte sequences (:class:`bytes` objects),
 
598
byte arrays (:class:`bytearray` objects), lists, tuples, and range objects.  For
 
599
other containers see the built in :class:`dict` and :class:`set` classes, and
 
600
the :mod:`collections` module.
 
601
 
572
602
 
573
603
.. index::
574
604
   object: sequence
575
605
   object: string
576
606
   object: bytes
577
 
   object: buffer
 
607
   object: bytearray
578
608
   object: tuple
579
609
   object: list
580
610
   object: range
845
875
   otherwise. Decimal characters include digit characters, and all characters
846
876
   that that can be used to form decimal-radix numbers, e.g. U+0660,
847
877
   ARABIC-INDIC DIGIT ZERO.
848
 
   
 
878
 
849
879
 
850
880
.. method:: str.isdigit()
851
881
 
873
903
   that have the Unicode numeric value property, e.g. U+2155,
874
904
   VULGAR FRACTION ONE FIFTH.
875
905
 
876
 
   
 
906
 
877
907
.. method:: str.isprintable()
878
908
 
879
909
   Return true if all characters in the string are printable or the string is
1086
1116
.. method:: str.translate(map)
1087
1117
 
1088
1118
   Return a copy of the *s* where all characters have been mapped through the
1089
 
   *map* which must be a dictionary of Unicode ordinals(integers) to Unicode
 
1119
   *map* which must be a dictionary of Unicode ordinals (integers) to Unicode
1090
1120
   ordinals, strings or ``None``.  Unmapped characters are left untouched.
1091
1121
   Characters mapped to ``None`` are deleted.
1092
1122
 
1093
 
   A *map* for :meth:`translate` is usually best created by
1094
 
   :meth:`str.maketrans`.
1095
 
 
1096
 
   You can use the :func:`maketrans` helper function in the :mod:`string` module to
1097
 
   create a translation table. For string objects, set the *table* argument to
1098
 
   ``None`` for translations that only delete characters:
 
1123
   You can use :meth:`str.maketrans` to create a translation map from
 
1124
   character-to-character mappings in different formats.
1099
1125
 
1100
1126
   .. note::
1101
1127
 
1448
1474
   example, sort by department, then by salary grade).
1449
1475
 
1450
1476
   While a list is being sorted, the effect of attempting to mutate, or even
1451
 
   inspect, the list is undefined.  The C implementation 
 
1477
   inspect, the list is undefined.  The C implementation
1452
1478
   makes the list appear empty for the duration, and raises :exc:`ValueError` if it
1453
1479
   can detect that the list has been mutated during a sort.
1454
1480
 
1498
1524
   >>> bytes.fromhex('f0 f1f2  ')
1499
1525
   b'\xf0\xf1\xf2'
1500
1526
 
1501
 
.. XXX verify/document translate() semantics!
1502
 
 
1503
 
   .. method:: bytes.translate(table[, delete])
1504
 
 
1505
 
   Return a copy of the bytes object where all bytes occurring in the optional
1506
 
   argument *delete* are removed, and the remaining bytes have been mapped
1507
 
   through the given translation table, which must be a bytes object of length
1508
 
   256.
1509
 
 
1510
 
   You can use the :func:`maketrans` helper function in the :mod:`string` module to
1511
 
   create a translation table.
1512
 
 
1513
 
   .. XXX a None table doesn't seem to be supported
1514
 
      Set the *table* argument to ``None`` for translations that only delete characters::
1515
 
 
1516
 
         >>> 'read this short text'.translate(None, 'aeiou')
1517
 
         'rd ths shrt txt'
 
1527
The translate method differs in semantics from the version available on strings:
 
1528
 
 
1529
.. method:: bytes.translate(table[, delete])
 
1530
 
 
1531
   Return a copy of the bytes or bytearray object where all bytes occurring in
 
1532
   the optional argument *delete* are removed, and the remaining bytes have been
 
1533
   mapped through the given translation table, which must be a bytes object of
 
1534
   length 256.
 
1535
 
 
1536
   You can use the :func:`string.maketrans` helper function to create a
 
1537
   translation table.
 
1538
 
 
1539
   Set the *table* argument to ``None`` for translations that only delete
 
1540
   characters::
 
1541
 
 
1542
      >>> b'read this short text'.translate(None, b'aeiou')
 
1543
      b'rd ths shrt txt'
1518
1544
 
1519
1545
 
1520
1546
.. _types-set:
1597
1623
   .. method:: union(other, ...)
1598
1624
               set | other | ...
1599
1625
 
1600
 
      Return a new set with elements from both sets.
 
1626
      Return a new set with elements from the set and all others.
1601
1627
 
1602
1628
   .. method:: intersection(other, ...)
1603
1629
               set & other & ...
1604
1630
 
1605
 
      Return a new set with elements common to both sets.
 
1631
      Return a new set with elements common to the set and all others.
1606
1632
 
1607
1633
   .. method:: difference(other, ...)
1608
1634
               set - other - ...
1781
1807
 
1782
1808
      Return the item of *d* with key *key*.  Raises a :exc:`KeyError` if *key* is
1783
1809
      not in the map.
1784
 
      
 
1810
 
1785
1811
      If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
1786
1812
      is not present, the ``d[key]`` operation calls that method with the key *key*
1787
1813
      as argument.  The ``d[key]`` operation then returns or raises whatever is
2123
2149
   positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the
2124
2150
   current position) and ``os.SEEK_END`` or ``2``  (seek relative to the file's
2125
2151
   end).  There is no return value.
2126
 
   
 
2152
 
2127
2153
   For example, ``f.seek(2, os.SEEK_CUR)`` advances the position by two and
2128
2154
   ``f.seek(-3, os.SEEK_END)`` sets the position to the third to last.
2129
2155