zoukankan      html  css  js  c++  java
  • 表达式,运算符和布尔值

    True ,任意非零数值,非空的字符串,列表,元组或字典都将返回True。

    False,0,None,空的列表,元组和字典都将返回False。

    布尔表达式从左至右进行求值,而且只有在需要时才会计算右边的操作数。例如表达式a and b,只有当a为True时才会计算b。这有时称为短路求值。

    圆括号时明确和改变表达式运算顺序的利器。

    一,算术运算符

    (1)+

    In [2]: [1,2,3]+[4,5,6]     连接两个列表
    Out[2]: [1, 2, 3, 4, 5, 6]
    
    In [3]: (1,2,3)+(4,)        连接两个元组
    Out[3]: (1, 2, 3, 4)
    In [4]: '1234'+'asdf'       连接两个字符串
    Out[4]: '1234asdf'
    In [5]: True+3               #True=1
    Out[5]: 4

    (2)*

    复制代码
    In [6]: False*3 #False=0
    Out[6]: 0
    
    In [7]: [1,2,3]*3
    Out[7]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    In [8]: (1,2,3)*3
    Out[8]: (1, 2, 3, 1, 2, 3, 1, 2, 3)
    
    In [9]: 'asdf'*3
    Out[9]: 'asdfasdfasdf'
    复制代码

    (3)/ 除 //算术求整商

    复制代码
    In [10]: 3/2
    Out[10]: 1.5
    
    In [11]: 12//5
    Out[11]: 2
    
    In [12]: 12.0//5
    Out[12]: 2.0
    
    In [13]: -12//5  向下取整,例如5*(-3)+3=-12   5*(-2)-2=-12
    Out[13]: -3
    
    In [14]: -12/5
    Out[14]: -2.4
    复制代码


    (4)% 余数运算和字符串格式化   格式化不熟,另行整理下

    复制代码
    In [15]: 10%3
    Out[15]: 1
    
    In [16]: 10.0%3
    Out[16]: 1.0
    
    In [17]: '%c,%d'%(65,65)
    Out[17]: 'A,65'
    
    In [18]: '%f,%s'%(66,68)
    Out[18]: '66.000000,68'
    复制代码
     
     
    max = 10
    sum = 0
    extra = 0
    
    for num in range(1, max):
        if num % 2 and not num % 3 :   num % 2 为真 即为奇数 1真0假; num % 3 为真 ,为真即不为0 ,就是不是3的倍数,加not ,就是3的倍数
            sum += num
        else:
            extra += num
    
    print(sum) sum为3+9=12
    print(extra) 其余的数之和 33

    (5)** 幂乘 等于pow()

    复制代码
    In [20]: 3**3
    Out[20]: 27
    
    In [21]: pow(3,3)
    Out[21]: 27
    
    In [22]: pow(3,3,10)   3**3%10
    Out[22]: 7
    
    In [23]: 9**0.5
    Out[23]: 3.0
    复制代码

    二,关系运算符

    复制代码
    In [24]: 1<2<4
    Out[24]: True
    
    In [25]: {1,2,3}<{1,2,3,4}
    Out[25]: True
    
    In [26]: [2,3,4]<[1,2,3,4]
    Out[26]: False
    
    In [27]: [2,3,4]<[2,3,4,5]
    Out[27]: True
    
    In [28]: [1,2,3]<[1,2,4]
    Out[28]: True
    
    In [29]: {2,3,4}<{2,3,4,5}
    Out[29]: True
    
    In [30]: (1,2,3)<(1,2,3,4)
    Out[30]: True
    
    In [31]: (2,3,4)<(1,2,3,4)
    Out[31]: False
    
    In [33]: (1,2,3)==(3,2,1)          元组,列表是有顺序的,集合时没有顺序的
    Out[33]: False
    
    In [34]: [1,2,3]==[3,2,1]
    Out[34]: False
    
    In [35]: {1,2,3}=={3,2,1}
    Out[35]: True
    复制代码


    三,in 成员测试运算符 is 同一性测试运算符

    复制代码
    In [36]: 3 in {1,2,3}
    Out[36]: True
    
    In [37]: 3 in [1,2,3]
    Out[37]: True
    
    In [38]: 3 in (1,2,3)
    Out[38]: True
    
    In [39]: 'as' in 'asdf'
    Out[39]: True
    
    In [40]: x=[1,2,3]
    
    In [41]: y=[1,2,3]
    
    In [42]: x is y  如果两个对象是同一个,两者具有相同的内存地址
    Out[42]: False
    
    In [43]: x==y
    Out[43]: True
    
    In [44]: x[1] is y[1]
    Out[44]: True
    
    In [45]: x[1]==y[1]
    Out[45]: True
    复制代码
    复制代码
    In [46]: x=[123,123,123]
    
    In [47]: x[0] is x[1]   基于值的内存管理,用一个值在内存中只有一份
    Out[47]: True
    
    In [48]: x[1]==x[2]
    Out[48]: True
    复制代码
    复制代码
    In [49]: x=[1,2,3]
    
    In [50]: y=x                x和y指向同一个对象
    
    In [51]: y is x
    Out[51]: True
    
    In [52]: x.append(100)
    
    In [53]: x
    Out[53]: [1, 2, 3, 100]
    
    In [54]: y
    Out[54]: [1, 2, 3, 100] 对x进行操作会对y造成同样的影响
    复制代码

    四,位运算符与集合运算符

    复制代码
    In [55]: {1,2,3}|{3,4,5}   去重求并集
    Out[55]: {1, 2, 3, 4, 5}
    
    In [56]: {1,2,3}&{3,4,5} 求交集
    Out[56]: {3}
    
    In [57]: {1,2,3}^{3,4,5} 对称差集
    Out[57]: {1, 2, 4, 5}
    
    In [58]: {2,3,4}-{2,6,7}  差集
    Out[58]: {3, 4}
    复制代码

    五,逻辑运算符 not and or
    运算符and和or不一定会返回True或False,而是得到最后一个被计算的表达式的值,但是运算符not一定会返回True或False.

    复制代码
    In [59]: 3>5 and a>7  没有定义a 记住短路规则
    Out[59]: False
    
    In [60]: 3>5 or a>7
    Traceback (most recent call last):
    
      File "<ipython-input-60-6dac9f78ce10>", line 1, in <module>
        3>5 or a>7
    
    NameError: name 'a' is not defined
    
    In [61]: 3<5  or a>7
    Out[61]: True
    
    In [62]: 3 and 5   短路规则
    Out[62]: 5
    
    In [63]: 3 or 5
    Out[63]: 3
    
    In [64]: 3 not in [1,2,3]  not的计算结果只能是True或者False
    Out[64]: False
    
    In [65]: not 3
    Out[65]: False
    
    In [66]: not []
    Out[66]: True
    复制代码

     六,比较运算符

    当用运算符比较两个值时,结果是一个逻辑值,True或者False。

  • 相关阅读:
    ajax 异步问题
    mysql update 报 You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode
    JSON string 和 object 转换
    Mybatis insert 返回主键
    switch case 的值
    $(this).attr("checked") 用jquery取checkbox的值 返回undefined
    7天入门JavaScript,第五天
    7天入门JavaScript,第四天
    7天入门JavaScript,第三天
    保持按钮的高亮状态
  • 原文地址:https://www.cnblogs.com/bchy/p/11685033.html
Copyright © 2011-2022 走看看