python基础之基本运算符

2022-10-17

这篇文章主要介绍了python基本运算符,实例分析了Python中返回一个返回值与多个返回值的方法,需要的朋友可以参考下

目录
  • Python基本运算符
    • 算数运算符
    • 比较运算符
    • 逻辑运算符
    • 赋值运算符
  • 总结

    Python基本运算符

    算数运算符

    # + - * / % ** //  算数运算符
    # 定义如下运算符
    a=7
    b=3
    print(a+b)
    print(a-b)
    print(a*b)
    print(a/b)
    print(a%b)
    print(a//b) # 地板除,相除取整,也可进行复合运算
    

    比较运算符

    # == != < > >= <= 比较运算符
    a,b=10,5
    print(a==b)
    print(a!=b)
    print(a<=b)
    print(a>=b)
    print(a<b)
    print(a>b)
    

    逻辑运算符

    # and or not 逻辑运算符
    # and  条件严格,都为真为真
    # or 都为假为假
    # not 非 真假切换,只需在前面加上not就可,对于需要取反操作的
    a,b,c,d=23,16,26,56
    print(a+b>c and c<d)
    print(a>b or c<d)
    print('--------not-------')
    print(a>b)
    print(not a>b)
    

    # 优先级
    # () -> not -> and -> or
    print (2>1 and 1<4 or 2<3 and 9>6 or 2<4)
    

    赋值运算符

    # 赋值运算 (算术运算的补充)
    # += -= /= %= **= *=
    a=10
    b=1
    c=4
    d=5
    a+=c
    print(a)
    b*=d
    print(d)
    d**=c  ##数字为几即为几次方
    print(d)
    

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注北冥有鱼的更多内容!

    《python基础之基本运算符.doc》

    下载本文的Word格式文档,以方便收藏与打印。