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.
176
176
single: __eq__() (instance method)
177
177
single: __ne__() (instance method)
178
178
single: __lt__() (instance method)
330
331
for well-defined conversions.
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.
337
338
Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
338
339
programming languages.
342
343
All :class:`numbers.Real` types (:class:`int` and
343
344
:class:`float`) also include the following operations:
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
+--------------------+------------------------------------+--------+
359
360
For additional numeric operations see the :mod:`math` and :mod:`cmath`
423
Additional Methods on Integer Types
424
-----------------------------------
426
.. method:: int.bit_length()
428
Return the number of bits necessary to represent an integer in binary,
429
excluding the sign and leading zeros::
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``.
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
450
.. versionadded:: 3.1
422
453
Additional Methods on Float
423
454
---------------------------
565
594
Sequence Types --- :class:`str`, :class:`bytes`, :class:`bytearray`, :class:`list`, :class:`tuple`, :class:`range`
566
595
==================================================================================================================
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.
873
903
that have the Unicode numeric value property, e.g. U+2155,
874
904
VULGAR FRACTION ONE FIFTH.
877
907
.. method:: str.isprintable()
879
909
Return true if all characters in the string are printable or the string is
1086
1116
.. method:: str.translate(map)
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.
1093
A *map* for :meth:`translate` is usually best created by
1094
:meth:`str.maketrans`.
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.
1448
1474
example, sort by department, then by salary grade).
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.
1498
1524
>>> bytes.fromhex('f0 f1f2 ')
1499
1525
b'\xf0\xf1\xf2'
1501
.. XXX verify/document translate() semantics!
1503
.. method:: bytes.translate(table[, delete])
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
1510
You can use the :func:`maketrans` helper function in the :mod:`string` module to
1511
create a translation table.
1513
.. XXX a None table doesn't seem to be supported
1514
Set the *table* argument to ``None`` for translations that only delete characters::
1516
>>> 'read this short text'.translate(None, 'aeiou')
1527
The translate method differs in semantics from the version available on strings:
1529
.. method:: bytes.translate(table[, delete])
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
1536
You can use the :func:`string.maketrans` helper function to create a
1539
Set the *table* argument to ``None`` for translations that only delete
1542
>>> b'read this short text'.translate(None, b'aeiou')
1597
1623
.. method:: union(other, ...)
1598
1624
set | other | ...
1600
Return a new set with elements from both sets.
1626
Return a new set with elements from the set and all others.
1602
1628
.. method:: intersection(other, ...)
1603
1629
set & other & ...
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.
1607
1633
.. method:: difference(other, ...)
1608
1634
set - other - ...
1782
1808
Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
1783
1809
not in the map.
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.
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.