Comparison operator summary from python

Aggregate (comparison operators return True/False) - output help('x ') can be queried

  • ==Equal to: compare whether the objects are equal. If it is an object, it means all the data are OK, such as collection, dictionary, etc.
  • ! = not equal to: whether the comparison object is not equal
  • >Greater than:
  • < less than:
  • >=Greater than:
  • Small = equal
    The content of comparison is all objects, that is, all data types can be compared
#I should have output all the comparators. Now I'm going to translate them. Today's one is over  
#It's all about grades
The following table summarizes the operator precedence in Python
#(the following table shows the priority of operators in python.), 
fromlowest precedence (least binding) to highest precedence (most
#(from low to high)
Operators in the same box have the same precedence. 
#Same priority in the same box
Unless the syntax is explicitly given, operators are binary.  
#Unless syntax is explicitly given, operators are binary
Operators inthe same box group left to right (except for exponentiation, which
groups from right to left).
#At the same level, comparisons are made from left to right, except that power functions (power functions) are right to left

Note that comparisons, membership tests, and identity tests, all have
the same precedence and have a left-to-right chaining feature as
described in the Comparisons section.
#Note that comparisons, membership tests, and identity tests all have the same priority and link functionality from left to right is described in the comparison section.

| Operator     Operator                                    | Description      describe                     |
| "lambda"   What is it?                                      | Lambda expression  Expression                  |
| "if""else"                                   | Conditional expression   Conditional expression         |
| "or"                or                            | Boolean OR                            |
| "and"                And                           | Boolean AND                           |
| "not" "x"             wrong                        | Boolean NOT                           |
| "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership     |
| ">=", "!=", "=="                                | tests and identity tests  
												  //Comparison includes membership test and identity test          |
| "|"                                             | Bitwise OR      Bitwise OR                      |
| "^"                                             | Bitwise XOR   Bit by bit???? I don't understand it here.                        |
| "&"                                             | Bitwise AND        Bitwise                   |
| "<<", ">>"                                      | Shifts     Displacement                           |
| "+", "-"                                        | Addition and subtraction               |
| "*", "@", "/", "//", "%"                        | Multiplication, matrix                |
|                                                 | multiplication, division, floor       |
|                                                 | division, remainder [5]               |
| "+x", "-x", "~x"                                | Positive, negative, bitwise NOT  Positive and negative      |
| "**"                                            | Exponentiation [6]                    |
| "await" "x"                                     | Await expression                      |
| "x[index]", "x[index:index]",                   | Subscription, slicing, call,          |
| "x(arguments...)", "x.attribute"                | attribute reference                   |
| "(expressions...)", "[expressions...]", "{key:  | Binding or tuple display, list        |
| value...}", "{expressions...}"                  | display, dictionary display, set      |
|                                                 | display                               |

-[ Footnotes ]-

[1] While "abs(x%y) < abs(y)" is true mathematically, for floats
    it may not be true numerically due to roundoff.  For example, and
    assuming a platform on which a Python float is an IEEE 754 double-
    precision number, in order that "-1e-100 % 1e100" have the same
    sign as "1e100", the computed result is "-1e-100 + 1e100", which
    is numerically exactly equal to "1e100".  The function
    "math.fmod()" returns a result whose sign matches the sign of the
    first argument instead, and so returns "-1e-100" in this case.
    Which approach is more appropriate depends on the application.
    [???I don't understand. I don't understand. I don't translate

[2] If x is very close to an exact integer multiple of y, it's
    possible for "x//y" to be one larger than "(x-x%y)//y" [This step can be used to find a negative number x-x%//y*y)]due to
    rounding.  In such cases, Python returns the latter result, in
    order to preserve that "divmod(x,y)[0] * y + x % y" be very close
    to "x".   

[3] The Unicode standard distinguishes between *code points* (e.g.
    U+0041) and *abstract characters* (e.g. "LATIN CAPITAL LETTER A").
    While most abstract characters in Unicode are only represented
    using one code point, there is a number of abstract characters
    that can in addition be represented using a sequence of more than
    one code point.  For example, the abstract character "LATIN
    CAPITAL LETTER C WITH CEDILLA" can be represented as a single
    *precomposed character* at code position U+00C7, or as a sequence
    of a *base character* at code position U+0043 (LATIN CAPITAL
    LETTER C), followed by a *combining character* at code position

    #. . . 
    The comparison operators on strings compare at the level of
    Unicode code points. This may be counter-intuitive to humans.  For
    example, ""\u00C7" == "\u0043\u0327"" is "False", even though both
    strings represent the same abstract character "LATIN CAPITAL
        #. . . 

    To compare strings at the level of abstract characters (that is,
    in a way intuitive to humans), use "unicodedata.normalize()".
    #. . . 

[4] Due to automatic garbage-collection, free lists, and the
    dynamic nature of descriptors, you may notice seemingly unusual
    behaviour in certain uses of the "is" operator, like those
    involving comparisons between instance methods, or constants.
    Check their documentation for more info.
    #is operator post complement

[5] The "%" operator is also used for string formatting; the same
    precedence applies.  
    # %It also has the function of formatting

[6] The power operator "**" binds less tightly than an arithmetic
    or bitwise unary operator on its right, that is, "2**-1" is "0.5".
    # The parameter of power is not too strict. It can be a negative number, a decimal number or a complex number

Related help topics: lambda, or, and, not, in, is, BOOLEAN, COMPARISON,
#Use help to learn the rest

Attention points

  • Can all data types be compared with equal or not equal?
  • Can't the other comparison operators be compared
  • The same data type can be used for comparison operators
  • Use comparison operators continuously?
  • Comparison between numbers and true/false
  • True/False initial caps equal true false
  • Number and string comparison
  • Comparison between strings
  • Comparison between different types
  • Object comparison object id comparison
  • Floating point comparison
  • ==Whether these are similar to js with type hook

=Conclusion (! =)

  • ==! = it is to compare whether the values are equal or not, which is called equivalent verification. As long as the values are equal, it does not compare the memory address
  • Is is to determine whether the value is the same or whether it comes from the same object (whether the memory address is the same)
  • True/False is different from True/False. Note that uppercase is defined
  • Comparative symbols can also be used in combination, equivalent to and
  • True =0.False=1 can be compared with a number
  • Number cannot be compared with string type 1=='1' false
  • When comparing floating-point numbers, they don't meet the strict requirements of precision. 1.1==1.1 true
  • When the precision of a floating-point number reaches 18 bits, the following minuteness will be ignored
    Print (1.00000000000000000000001 = = 1.00000000000000003) 񖓿true 18 bits
#Summary of comparison operators

# ==What does comparison mean
print(1==1) #True
print(1.1==1.1) #Does True have precision
print(1.00000000000000000000001==1.00000000000000000000002) #When the precision is more than a little, it will be considered equal
print(1.0000001==1.0000003) #false
print(1.00000000000000001==1.00000000000000003)#true 18 bits
print(1.1=='1.1') #false no conversion
print('a'=='A') #false case sensitive
print(1.1!='1.1') #true
print((1,2,3)==set('123')) #false
print(set('123')) #{'2','3','1'}
print({1,2,3}=## Title=set('123')) #false
print({'1','2','3'}==set('123')) #true
print({'1','2','3'}is set('123'))#false determine the memory address of the object
#print('1'==true) true is not defined
print ('1'==True) #False value is different
print(1==True) #True true = 1 so equal
print(1.123==1.123) #true no precision

> ,<, >=, <=

The four are similar, just study one

  • Number string cannot be compared directly not supported
    print(98<'a') #'<' not supported between instances of 'int' and 'str'
  • All data types can be compared and compared in depth. The first result leads to conclusion 1,
  • Floating point number comparison size: can be compared, normal comparison, ignoring the precision problem (18 bits)
  • When the precision of floating-point numbers is large, the comparison of the following decimals is ignored, with 18 digits
  • String and number are comparable: non comparable Python 3 does not support, 2 should support, one by one for comparison.
  • Whether the string size is comparable: compare by ASCII code
  • Whether the forms such as set can be compared: the object type can be compared, but python cancels some comparison forms

python's comparison will check all parts of the composite object until the result is obtained. It will automatically traverse all nested data structures and compare as many as there are. The difference found for the first time will determine that the comparison result is based on the result of the first comparison, regardless of the following

  • Numbers are compared by relative size
  • Strings are compared one by one in ascii encoding order
  • List and tuple compare the contents of each part from left to right, one-to-one, according to the ASCII table
  • The dictionary is compared by the sorted (key, value) list, which is not supported by Python 3
  • Digital hybrid python3 is not supported
    Source text
    ASCII table
# > < >= <=
print(1>2) #false
#Print (1 >'1 ') cannot be compared with str and int

print(1>2<4) # 1>2 and 2<4 false
print({1,2,3}>={1,2,3,4}) #false
print({1,2,.3,4}>={1,2,3}) #true
print({1,4}>={1,2}) #Is false more than number? "
print({1,2,3}>={1,2}) #true
print({1,2,2}>={1,2,2,2,2}) #true is not the number of comparison

print(33<99) #true
print('abc'<'ac') # a=97 b=98 c =99 a=a b<c true
print('cab'<'ab') #false  c>a a=a b=b
print('ca'<'bd') # One by one comparison is determined by the first value
print('1'<'a') #true a=97
print('98'<'a')# 9 < a true
print(1.000000000000000005>1.000000000000000001) #false indeed ignores precision
print('A'<'a') #true Ascii
#print({'a':1,'b':2} > {'a':1,'B':2}) #a 97 b:98 B66 true python2 not supported
#Print (1 < 'spam') is not supported

Operational order

If there are more than three judgments in the same operation class, from left to right, it is equivalent to comparing two and then doing the and operation.

Published 31 original articles, won praise 2, visited 9491
Private letter follow

Tags: Python ascii Lambda less

Posted on Wed, 12 Feb 2020 22:32:53 -0500 by artin