Python基础:Python运算符
Python运算符
Python 运算符
运算符用于对变量和值执行操作。
Python 将运算符分为以下组:
- 算术运算符
- 分配运算符
- 比较运算符
- 逻辑运算符
- 标识运算符
- 会员运算符
- 位运算符
Python 算术运算符
算术运算符与数值一起使用,以执行常见的数学运算:
| Operator | Name | Example | | + | Addition | x + y | | - | Subtraction | x - y | | * | Multiplication | x * y | | / | Division | x / y | | % | Modulus | x % y | | ** | Exponentiation | x ** y | | // | Floor division | x // y |
Python 分配运算符
赋值运算符用于将值分配给变量:
| Operator | Example | Same As | | = | x = 5 | x = 5 | | += | x += 3 | x = x + 3 | | -= | x -= 3 | x = x - 3 | | *= | x *= 3 | x = x * 3 | | /= | x /= 3 | x = x / 3 | | %= | x %= 3 | x = x % 3 | | //= | x //= 3 | x = x // 3 | | **= | x **= 3 | x = x ** 3 | | &= | x &= 3 | x = x & 3 | | |= | x |= 3 | x = x | 3 | | ^= | x ^= 3 | x = x ^ 3 | | »= | x »= 3 | x = x » 3 | | «= | x «= 3 | x = x « 3 |
Python 比较运算符
比较运算符用于比较两个值:
| Operator | Name | Example | | == | Equal | x == y | | != | Not equal | x != y | | > | Greater than | x > y | | < | Less than | x < y | | >= | Greater than or equal to | x >= y | | <= | Less than or equal to | x <= y |
Python 逻辑运算符
逻辑运算符用于组合条件语句:
| Operator | Description | Example | | and | Returns True if both statements are true | x < 5 and x < 10 | | or | Returns True if one of the statements is true | x < 5 or x < 4 | | not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Python 标识运算符
标识运算符用于比较对象,而不是它们相等,但如果它们实际上是同一个对象,则具有相同的内存位置:
| Operator | Description | Example | | is | Returns True if both variables are the same object | x is y | | is not | Returns True if both variables are not the same object | x is not y |
Python 成员资格运算符
成员资格运算符用于测试对象中是否显示序列:
| Operator | Description | Example | | in | Returns True if a sequence with the specified value is present in the object | x in y | | not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
Python 位运算符
位运算符用于比较(二进制)数字:
| Operator | Name | Description | | & | AND | Sets each bit to 1 if both bits are 1 | | | | OR | Sets each bit to 1 if one of two bits is 1 | | ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | | ~ | NOT | Inverts all the bits | | « | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | | » | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
- 原文作者:知识铺
- 原文链接:https://geek.zshipu.com/post/python/Python%E5%9F%BA%E7%A1%80Python%E8%BF%90%E7%AE%97%E7%AC%A6_20201025191937-ql8wcyv.sy/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。
- 免责声明:本页面内容均来源于站内编辑发布,部分信息来源互联网,并不意味着本站赞同其观点或者证实其内容的真实性,如涉及版权等问题,请立即联系客服进行更改或删除,保证您的合法权益。转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 sblig@126.com