• python运算符


    什么是操作符?

    简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。

    • 算术运算符

    • 比较(即关系)运算符

    • 赋值运算符

    • 逻辑运算符

    • 位运算符

    • 会员操作符

    • 标识操作符

    让我们逐一看看所有的运算符。

    Python算术运算符:

    假设变量a持有10和变量b持有20,则:

    操作符描述符例子
    + 加法 - 对操作符的两侧增加值 a + b = 30
    - 减法 - 减去从左侧操作数右侧操作数 a - b = -10
    * 乘法 - 相乘的运算符两侧的值 a * b = 200
    / 除 - 由右侧操作数除以左侧操作数 b / a = 2
    % 模 - 由右侧操作数和余返回除以左侧操作数 b % a = 0
    ** 指数- 执行对操作指数(幂)的计算 a**b = 10 的幂 20
    // 地板除 - 操作数的除法,其中结果是将小数点后的位数被除去的商。 9//2 =  4 而 9.0//2.0 = 4.0

    Python的比较操作符:

    假设变量a持有10和变量b持有20,则:

    运算符描述示例
    == 检查,两个操作数的值是否相等,如果是则条件变为真。 (a == b) 不为 true.
    != 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 (a != b) 为 true.
    <> 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 (a <> b) 为 true。这个类似于 != 运算符
    > 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 (a > b) 不为 true.
    < 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 (a < b) 为 true.
    >= 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 (a >= b) 不为 true.
    <= 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 (a <= b) 为 true.

    Python赋值运算符:

    假设变量持有10和变量b持有20,则:

    运算符描述示例
    = 简单的赋值运算符,赋值从右侧操作数左侧操作数 c = a + b将指定的值 a + b 到  c
    += 加法AND赋值操作符,它增加了右操作数左操作数和结果赋给左操作数 c += a 相当于 c = c + a
    -= 减AND赋值操作符,它减去右边的操作数从左边操作数,并将结果赋给左操作数 c -= a 相当于 c = c - a
    *= 乘法AND赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数 c *= a 相当于 c = c * a
    /= 除法AND赋值操作符,它把左操作数与正确的操作数,并将结果赋给左操作数 c /= a 相当于= c / a
    %= 模量AND赋值操作符,它需要使用两个操作数的模量和分配结果左操作数 c %= a is equivalent to c = c % a
    **= 指数AND赋值运算符,执行指数(功率)计算操作符和赋值给左操作数 c **= a 相当于 c = c ** a
    //= 地板除,并分配一个值,执行地板除对操作和赋值给左操作数 c //= a 相当于 c = c // a

    试试下面的例子就明白了所有在Python编程语言可供选择的赋值运算符:

    #!/usr/bin/python
    
    a =21
    b =10
    c =0
    
    c = a + b
    print"Line 1 - Value of c is ", c
    
    c += a
    print"Line 2 - Value of c is ", c 
    
    c *= a
    print"Line 3 - Value of c is ", c 
    
    c /= a 
    print"Line 4 - Value of c is ", c 
    
    c  =2
    c %= a
    print"Line 5 - Value of c is ", c
    
    c **= a
    print"Line 6 - Value of c is ", c
    
    c //= aprint"Line 7 - Value of c is ", c

    当执行上面的程序,它会产生以下结果:

    Line 1 - Value of c is 31
    Line 2 - Value of c is 52
    Line 3 - Value of c is 1092
    Line 4 - Value of c is 52
    Line 5 - Value of c is 2
    Line 6 - Value of c is 2097152
    Line 7 - Value of c is 99864

    Python位运算符:

    位运算符作用于位和位操作执行位。假设,如果a =60;且b =13;现在以二进制格式它们将如下:

    a = 0011 1100

    b = 0000 1101

    -----------------

    a&b = 0000 1100

    a|b = 0011 1101

    a^b = 0011 0001

    ~a  = 1100 0011

    http://www.cnblogs.com/roucheng/

    Python语言支持下位运算符

    操作符描述示例
    & 二进制和复制操作了一下,结果,如果它存在于两个操作数。 (a & b) = 12 即 0000 1100
    | 二进制或复制操作了一个比特,如果它存在一个操作数中。 (a | b) = 61 即 0011 1101
    ^ 二进制异或运算符的副本,如果它被设置在一个操作数而不是两个比特。 (a ^ b) =  49 即  0011 0001
    ~ 二进制的补运算符是一元的,并有“翻转”位的效果。 (~a ) =  -61 即 1100 0011以2的补码形式由于带符号二进制数。
    << 二进位向左移位运算符。左操作数的值左移由右操作数指定的位数。 a << 2 = 240 即 1111 0000
    >> 二进位向右移位运算符。左操作数的值是由右操作数指定的位数向右移动。 a >> 2 = 15 即 0000 1111

    Python逻辑运算符:

    Python语言支持以下逻辑运算符。假设变量a持有10和变量b持有20则:

    运算符描述示例
    and 所谓逻辑与运算符。如果两个操作数都是真的,那么则条件成立。 (a and b) 为 true.
    or 所谓逻辑OR运算符。如果有两个操作数都是非零然后再条件变为真。 (a or b) 为 true.
    not 所谓逻辑非运算符。用于反转操作数的逻辑状态。如果一个条件为真,则逻辑非运算符将返回false。 not(a and b) 为 false.

    Python成员运算符:

    除了前面讨论的运算符,Python成员运算符,在一个序列中成员资格的测试,如字符串,列表或元组。有两个成员运算符解释如下:

    操作符描述示例
    in 计算结果为true,如果它在指定找到变量的顺序,否则false。 x在y中,在这里产生一个1,如果x是序列y的成员。
    not in 计算结果为true,如果它不找到在指定的变量顺序,否则为false。 x不在y中,这里产生结果不为1,如果x不是序列y的成员。

    试试下面的例子就明白了所有的Python编程语言提供会员运算符:

    #!/usr/bin/python
    
    a =10
    b =20
    list =[1,2,3,4,5];if( a in list ):print"Line 1 - a is available in the given list"else:print"Line 1 - a is not available in the given list"if( b notin list ):print"Line 2 - b is not available in the given list"else:print"Line 2 - b is available in the given list"
    
    a =2if( a in list ):print"Line 3 - a is available in the given list"else:print"Line 3 - a is not available in the given list"

    当执行上面的程序它会产生以下结果:

    Line 1 - a is not available in the given list
    Line 2 - b is not available in the given list
    Line 3 - a is available in the given list

    Python标识运算符:

    标识符比较两个对象的内存位置。两个运算符标识解释如下:

    运算符描述例子
    is 计算结果为true,如果操作符两侧的变量指向相同的对象,否则为false。 x是y,这里结果是1,如果id(x)的值为id(y)。
    is not 计算结果为false,如果两侧的变量操作符指向相同的对象,否则为true。 x不为y,这里结果不是1,当id(x)不等于id(y)。

    试试下面的例子就明白了所有Python编程语言提供的标识运算符:

    #!/usr/bin/python
    
    a =20
    b =20if( a is b ):print"Line 1 - a and b have same identity"else:print"Line 1 - a and b do not have same identity"if( id(a)== id(b)):print"Line 2 - a and b have same identity"else:print"Line 2 - a and b do not have same identity"
    
    b =30if( a is b ):print"Line 3 - a and b have same identity"else:print"Line 3 - a and b do not have same identity"if( a isnot b ):print"Line 4 - a and b do not have same identity"else:print"Line 4 - a and b have same identity"

    当执行上面的程序它会产生以下结果:

    Line 1 - a and b have same identity
    Line 2 - a and b have same identity
    Line 3 - a and b do not have same identity
    Line 4 - a and b do not have same identity



    Python运算符优先级

    下表列出了所有运算符从最高优先级到最低。

    运算符描述
    ** 幂(提高到指数)
    ~ + - 补码,一元加号和减号(方法名的最后两个+@和 - @)
    * / % // 乘,除,取模和地板除
    + - 加法和减法
    >> << 左,右按位转移
    & 位'AND'
    ^ | 按位异'或`'和定期`或'
    <= < > >= 比较运算符
    <> == != 等式运算符
    = %= /= //= -= += *= **= 赋值运算符
    is is not 标识运算符
    in not in 成员运算符
    not or and 逻辑运算符


    转自: http://hovertree.com/h/bjaf/yunsuanfu.htm

    前端:http://www.cnblogs.com/roucheng/p/texiao.html

  • 相关阅读:
    C#音频截取与原文匹配2:使用ffmpeg处理音频文件
    C#音频截取与原文匹配
    Redis报错: StackExchange.Redis.RedisServerException: Endpoint 39.105.22.111:7200 serving hashslot 12448 is not reachable at this point of time.
    kafka单机安装部署
    zookeeper部署
    mysql-5.7.15编译安装
    centos7安装sqlserver
    redisearch模块安装
    yum安装软件后保留rpm包
    shell读取配置文件
  • 原文地址:https://www.cnblogs.com/roucheng/p/pythonyunsuan.html
Copyright © 2020-2023  润新知