来自python的【比较运算符总结】

总类 (比较运算符返回的都是True/False) — 输出help(‘x’) 都可以查询

  • == 等于 :比较对象是否相等 如果是对象的话,就意味着所有的数据都行,比如 集合、字典等。
  • != 不等于:比较对象是否不相等
  • > 大于 :
  • < 小于:
  • >=大等于:
  • <=小等于
    比较的内容都是对象,也就是能够比较所有数据类型
#应该是把比较符的都输出了 现在我要来翻译一下了 翻译一下 今天的这个就先结束  
#讲的都是运算等级
The following table summarizes the operator precedence in Python
#(下表说了运算符在python中的优先级), 
fromlowest precedence (least binding) to highest precedence (most
binding)
#(由低到高).
Operators in the same box have the same precedence. 
#同一个框中有同样的优先级
Unless the syntax is explicitly given, operators are binary.  
#除非明确给出语法,否则操作符是二进制的
Operators inthe same box group left to right (except for exponentiation, which
groups from 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.
#请注意,比较、成员资格测试和身份测试都有具有相同的优先级和从左到右的链接功能在比较部分进行了描述。

+-------------------------------------------------+---------------------------------------+
| Operator     操作符                                    | Description      描述                     |
|=================================================|=======================================|
| "lambda"   啥玩意                                      | Lambda expression  表达式                  |
+-------------------------------------------------+---------------------------------------+
| "if""else"                                   | Conditional expression   条件表达式         |
+-------------------------------------------------+---------------------------------------+
| "or"| Boolean OR                            |
+-------------------------------------------------+---------------------------------------+
| "and"| Boolean AND                           |
+-------------------------------------------------+---------------------------------------+
| "not" "x"| Boolean NOT                           |
+-------------------------------------------------+---------------------------------------+
| "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership     |
| ">=", "!=", "=="                                | tests and identity tests  
												  比较包括会员测试和身份测试          |
+-------------------------------------------------+---------------------------------------+
| "|"                                             | Bitwise OR      按位或                      |
+-------------------------------------------------+---------------------------------------+
| "^"                                             | Bitwise XOR   按位非???? 这里不懂                        |
+-------------------------------------------------+---------------------------------------+
| "&"                                             | Bitwise AND        按位与                   |
+-------------------------------------------------+---------------------------------------+
| "<<", ">>"                                      | Shifts     移位                           |
+-------------------------------------------------+---------------------------------------+
| "+", "-"                                        | Addition and subtraction               |
+-------------------------------------------------+---------------------------------------+
| "*", "@", "/", "//", "%"                        | Multiplication, matrix                |
|                                                 | multiplication, division, floor       |
|                                                 | division, remainder [5]               |
+-------------------------------------------------+---------------------------------------+
| "+x", "-x", "~x"                                | Positive, negative, bitwise NOT  正负取反      |
+-------------------------------------------------+---------------------------------------+
| "**"                                            | 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.
    【???不懂 看懂了 没理解什么意思 不翻译了】

[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" 【这步可以用来负数求余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
    U+0327 (COMBINING CEDILLA).

    #。。。
    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
    LETTER C WITH CEDILLA”.
        #。。。

    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 运算符  后期补

[5] The "%" operator is also used for string formatting; the same
    precedence applies.  
    # %也有进行格式化的作用

[6] The power operator "**" binds less tightly than an arithmetic
    or bitwise unary operator on its right, that is, "2**-1" is "0.5".
    # 幂次的参数 没有太严格的限制 可以是负数  可以是小数 都可以 复数都行

Related help topics: lambda, or, and, not, in, is, BOOLEAN, COMPARISON,
BITWISE, SHIFTING, BINARY, FORMATTING, POWER, UNARY, ATTRIBUTES,
SUBSCRIPTS, SLICINGS, CALLS, TUPLES, LISTS, DICTIONARIES, COMPARISON
#使用help进行了解其余的

注意点

  • 等于与不等于 可以比较所有的数据类型吗?
  • 其余比较运算符是不可以比较吗
  • 同一个数据类型 ,才能进行比较运算符
  • 连续使用比较运算符?
  • 数字与true/false比较
  • True/False 首字母大写是否相等 true false
  • 数字和字符串比较
  • 字符串之间的比较
  • 各种类型之间的比较
  • 对象的比较 对象id的比较
  • 浮点数的比较
  • == 这些是否和类型挂钩 类似js

= 结论 (!=)

  • == != 比较的是值是否相等,叫做等值校验,只要是值相等,并不比较内存地址
  • is 是判断是否 值是否相同,是否来自同一个对象(内存地址是否相同)
  • true/ false 与True/False不等,注意大写才是定义的
  • 比较符号也能进行连用,相当于and
  • True =0.False=1 能够与数字进行比较
  • 数字与字符串数型不能比较 1=='1' false
  • 浮点数进行比较,居然没有遭受精度的严格要求,1.1==1.1 true
  • 浮点数的精度到达18位的时候,会忽略后面的微小数
    print(1.00000000000000001==1.00000000000000003)#true 18位
#比较运算符 总结


# == 比较指是多少
print(1==1) #True
print(1.1==1.1) #True  不存在精度吗
print(1.00000000000000000000001==1.00000000000000000000002) #当精度超过一些的时候还是会被认为是相等的值
print(1.0000001==1.0000003) #false
print(1.000000001==1.000000003)#false
print(1.00000000001==1.00000000003)#false
print(1.0000000000001==1.0000000000003)#false
print(1.000000000000001==1.000000000000003)#false
print(1.00000000000000001==1.00000000000000003)#true 18位
print(1.0000000000000001==1.0000000000000003)#false
print(1.1=='1.1') #false 没有转换
print('a'=='A') #false 区分大小写
print(1.1!='1.1') #true
print([1,2,3]==[1,2,3])#true
print((1,2,3)==set('123')) #false
print(set('123')) #{'2','3','1'}
print({1,2,3}=## 标题=set('123')) #false
print({'1','2','3'}==set('123')) #true
print({'1','2','3'}is set('123'))#false  判断对象内存地址是多少
#print('1'==true) true is not defined
print ('1'==True) #False 值不同
print(1==True) #True  True=1 所以相等
print(1.123==1.123) #true 无精度


> 、<、 >=、 <=

四者类似、只要研究一个即可、

  • 数字字符串不能直接比较 不支持
    print(98<'a') #'<' not supported between instances of 'int' and 'str'
  • 所有数据类型都能够进行比较,并且深挖比较,由第一个结果得出结论1,
  • 浮点数比较大小:可以比较,正常比较,精度问题忽略(18位)
  • 浮点数精度大的时候是忽略后面的小数的 比较18位
  • 字符串与数字是可比:不可比较 python3不支持,2应该支持,一个一个进行比较。
  • 字符串大小是否可比:通过ASCII码比较
  • set 等形式是否可比:可以 是对象类型就可以比较 但是python取消了一些比较形式

python 的比较会检查复合对象的所有部分,直到得出结果为止,会自动遍历嵌套所有数据结构,有多少就比较多少,首次发现的差值会决定比较结果 以第一次比较的结果为准,不理后面的

  • 数字通过相对大小进行比较
  • 字符串按照ascii编码顺序 一个一个字符串比较
  • 列表和元组从左到右对每部分的内容进行比较 一对一的进行比对,根据ASCII表
  • 字典通过排序后的(键,值)列表进行比较,python3不支持
  • 数字混合python3不支持
    原文出处
    ASCII表
    在这里插入图片描述
# > < >= <=
print(1>2) #false
print(1<2)#True
#print(1>'1')  不能 str和int 不能比较
#print(help('>'))
#print(2**(1+2j))

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}) #false 比个数吗?》
print({1,2,3}>={1,2}) #true
print({1,2,2}>={1,2,2,2,2}) #true 不是比个数

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') # 一个一个进行比较 由第一个值决定
print('1'<'a') #true a=97
print('98'<'a')# 9 < a true
#print(98<'a')
print(1.1>1.01)#true
print(1.000000000000000005>1.000000000000000001) #false 果然忽略精度
print('A'<'a') #true Ascii
#print({'a':1,'b':2} > {'a':1,'B':2}) #a 97 b:98 B66 true python2 不支持
#print(1<'spam') 不支持


运算顺序

同一运算阶级、从左到右,如果是三个以上的判断,相当于两两比较 再进行and运算。

发布了35 篇原创文章 · 获赞 2 · 访问量 9814

猜你喜欢

转载自blog.csdn.net/qq_39532595/article/details/104247991