Teks sumber — English
Terjemahan — Bahasa Indonesia
There are eight comparison operations in Python. They all have the same priority (which is higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and y <= z``, except that *y* is evaluated only once (but in both cases *z* is not evaluated at all when ``x < y`` is found to be false).
Ada delapan operasi perbandingan di Python. Mereka semua memiliki prioritas yang sama (yang lebih tinggi dari operasi *Boolean*). Perbandingan dapat dirantai secara berubah-ubah; misalnya, ``x < y <= z`` setara dengan ``x < y and y <= z``, kecuali bahwa *y* dievaluasi hanya sekali (tetapi dalam kedua kasus *z* tidak dievaluasi sama sekali ketika ``x < y`` ditemukan bernilai salah).
Unless stated otherwise, objects of different types never compare equal. The ``==`` operator is always defined but for some object types (for example, class objects) is equivalent to :keyword:`is`. The ``<``, ``<=``, ``>`` and ``>=`` operators are only defined where they make sense; for example, they raise a :exc:`TypeError` exception when one of the arguments is a complex number.
A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``.
A right shift by *n* bits is equivalent to floor division by ``pow(2, n)``.
Equivalent to::
Setara dengan::
To clarify the above rules, here's some example Python code, equivalent to the built-in hash, for computing the hash of a rational number, :class:`float`, or :class:`complex`::
import sys, math
def hash_fraction(m, n):
"""Compute the hash of a rational number m / n.
Assumes m and n are integers, with n positive.
Equivalent to hash(fractions.Fraction(m, n)).
"""
P = sys.hash_info.modulus
# Remove common factors of P. (Unnecessary if m and n already coprime.)
while m % P == n % P == 0:
m, n = m // P, n // P
if n % P == 0:
hash_value = sys.hash_info.inf
else:
# Fermat's Little Theorem: pow(n, P-1, P) is 1, so
# pow(n, P-2, P) gives the inverse of n modulo P.
hash_value = (abs(m) % P) * pow(n, P - 2, P) % P
if m < 0:
hash_value = -hash_value
if hash_value == -1:
hash_value = -2
return hash_value
def hash_float(x):
"""Compute the hash of a float x."""
if math.isnan(x):
return object.__hash__(x)
elif math.isinf(x):
return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
else:
return hash_fraction(*x.as_integer_ratio())
def hash_complex(z):
"""Compute the hash of a complex number z."""
hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
# do a signed reduction modulo 2**sys.hash_info.width
M = 2**(sys.hash_info.width - 1)
hash_value = (hash_value & (M - 1)) - (hash_value & M)
if hash_value == -1:
hash_value = -2
return hash_value
For logical operations, use the :ref:`boolean operators <boolean>` ``and``, ``or`` and ``not``. When applying the bitwise operators ``&``, ``|``, ``^`` to two booleans, they return a bool equivalent to the logical operations "and", "or", "xor". However, the logical operators ``and``, ``or`` and ``!=`` should be preferred over ``&``, ``|`` and ``^``.
Raises a :exc:`GeneratorExit` exception at the point where the generator function is currently suspended (equivalent to calling ``throw(GeneratorExit)``).
Returns an awaitable that when run will throw a :exc:`GeneratorExit` into the underlying asynchronous generator function at the point where it is currently suspended (equivalent to calling ``athrow(GeneratorExit)``).
equivalent to adding *s* to itself *n* times
The *start* or *stop* arguments allow for efficient searching of subsections of the sequence, beginning at *start* and ending at *stop*. This is roughly equivalent to ``start + sequence[start:stop].index(value)``, only without copying any data.
Append *value* to the end of the sequence. This is equivalent to writing ``seq[len(seq):len(seq)] = [value]``.
Remove all items from *sequence*. This is equivalent to writing ``del sequence[:]``.
Create a shallow copy of *sequence*. This is equivalent to writing ``sequence[:]``.
Insert *value* into *sequence* at the given *index*. This is equivalent to writing ``sequence[index:index] = [value]``.
If at least one of *encoding* or *errors* is given, *object* should be a :term:`bytes-like object` (e.g. :class:`bytes` or :class:`bytearray`). In this case, if *object* is a :class:`bytes` (or :class:`bytearray`) object, then ``str(bytes, encoding, errors)`` is equivalent to :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes object underlying the buffer object is obtained before calling :meth:`bytes.decode`. See :ref:`binaryseq` and :ref:`bufferobjects` for information on buffer objects.
Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold` converts it to ``"ss"``. For example:
Return ``True`` if the string ends with the specified *suffix*, otherwise return ``False``. *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. Using *start* and *end* is equivalent to ``str[start:end].endswith(suffix)``. For example::
This is equivalent to:
Return the data in the buffer as a bytestring. This is equivalent to calling the :class:`bytes` constructor on the memoryview. ::
Equivalent to ``not key in d``.
Defines a union object which holds types *X*, *Y*, and so forth. ``X | Y`` means either X or Y. It is equivalent to ``typing.Union[X, Y]``. For example, the following function expects an argument of type :class:`int` or :class:`float`::
If you access a method (a function defined in a class namespace) through an instance, you get a special object: a :dfn:`bound method` (also called :ref:`instance method <instance-methods>`) object. When called, it will add the ``self`` argument to the argument list. Bound methods have two special read-only attributes: :attr:`m.__self__ <method.__self__>` is the object on which the method operates, and :attr:`m.__func__ <method.__func__>` is the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)`` is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)``.