zoukankan      html  css  js  c++  java
  • 周一04.1运算符

    基本运算符


    一、算数运算(以下假设变量:a=10,b=20)int,float=>数字类型

    +与*(了解)
    msg1='hello'                                   
    msg2='world'
    print(msg1 + msg2)
    print(msg1*10)

    w1=['a','b']
    w2=['c','d']
    print(w1 + w2)
    print(w1*3)

    二、比较运算(以下假设变量:a=10,b=20)

    了解: (是根据每个字符一个一个进行比较,有一个成立就输出结果)(适用于str、list、、、)

    m1='abcd'

    m2='z'

    print(m1>m2)

    三、赋值运算

    增量赋值

    age=18

    age+=1   #age=age+1     

    print(age)

    交叉赋值

    x=11
    y=22
    temp=x
    x=y
    y=temp
    x,y=y,x
    print(x,y)

    链式赋值

    x=10
    y=x
    z=y
    x=y=z=10
    print(x,y,z)

    解压赋值

    1. 列表的解压赋值
    w=['egon',18,'male',33333]
    a=w[0]
    b=w[1]
    c=w[2]
    d=w[3]
    a,b,c,d=w
    print(a,b,c,d)

    选择解压某些值,使用  _和*_

    salaries=[1.1,2.2,3.3,4.4,5.5]
    a,b,_,_,_=salaries
    a,b,*_=salaries
    *_,a,b=salaries
    a,*_,b=salaries
    print(a,b,)

    2. 字典的解压赋值
    dic={'aaa':1,'bbb':2,'ccc':3}
    x,y,z=dic
    print(x,y,z)  #输出内容是key

     四、逻辑运算

    and: 左右两个条件必须同时成立,最终结果才为True
    print(10 < 3 and 3 == 3)

    or: 左右两个条件只要有一个成立,最终结果就为True
    print(10 < 3 or 3 == 3)
    print(10 < 3 or 3 < 3)

    not:将紧跟其后的条件结果取反
    print(not 10 > 3)
    print(not 10 < 3 or 3 < 3)

    #三者的优先级从高到低分别是:not,or,and
    >>> 3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3
    False
    #最好使用括号来区别优先级,其实意义与上面的一样
    >>> (3>4 and 4>3) or ((1==3 and 'x' == 'x') or 3 >3)
    False 

     五、身份运算

    is比较的是id    

    ==比较的是值   # print(10 != 3)

     补充

  • 相关阅读:
    BZOJ2219数论之神——BSGS+中国剩余定理+原根与指标+欧拉定理+exgcd
    Luogu 3690 Link Cut Tree
    CF1009F Dominant Indices
    CF600E Lomsat gelral
    bzoj 4303 数列
    CF1114F Please, another Queries on Array?
    CF1114B Yet Another Array Partitioning Task
    bzoj 1858 序列操作
    bzoj 4852 炸弹攻击
    bzoj 3564 信号增幅仪
  • 原文地址:https://www.cnblogs.com/wanglimei/p/10197571.html
Copyright © 2011-2022 走看看