Comparison operators
The following table lists the standard item comparison operations that are built into
Python, to compare the value or identity of objects. Note that in Python 3 inequality
comparisons can only be made on objects of a comparable type. In Python 2 you can make
the comparison 2 < ‘a’ (even though it does not really make sense) but this is illegal in
Python 3.
Operator
Description
Example
==
Tests whether two operands (either side of the
operator) have the same value, giving True if
they do and False otherwise.
x = 3 * 5
if x == 15:
print("Equal")
!=
Tests whether two operands have different
values, giving True if they do and False
otherwise.
seq = 'GCGC'
if seq !=
'TATA':
print("Not
equal")
>
Tests whether the value of the first operand is
greater in value than the second, giving True if
it is and False otherwise.
x = 2**10
x > 1024
Evaluates to False;
2
10
is exactly
1024, not greater.
<
Tests whether the value of the first operand is
smaller in value than the second, giving True if
it is and False otherwise.
x = 2**10
x < 1025
Evaluates to True.
>=
Tests whether the value of the first operand is
Do'stlaringiz bilan baham: |