数学&逻辑运算符
比较运算符 = 关系运算符 成立返回的是True,不成立返回的是False == 等于 != 不等于 > 大于 < 小于 >= 大于等于 <= 小于等于 """ """ 算术运算符 加法 + 减法 - 乘法 * 除法 / 求余 % 求幂 ** 整除 // """ """ 赋值运算符 = 赋值 a = b + c += 加法赋值 a += b 等价于 a = a + b -= 减法赋值 a -= b 等价于 a = a - b *= 乘法赋值 a *= b 等价于 a = a * b /= 除法赋值 a /= b 等价于 a = a / b %= 求余赋值 a %= b 等价于 a = a % b **= 求幂赋值 a **= b 等价于 a = a ** b //= 整除赋值 a //= b 等价于 a = a // b """
# -*- coding: utf-8 -*- """ 位运算符 把数字当作二进制来进行计算的符号 二进制:0和1构成 十进制:0到9构成 十进制如何转化为二进制? 30的二进制? 30 % 2 = 0,15 15 % 2 = 1,7 7 % 2 = 1,3 3 % 2 = 1,1 1 % 2 = 1,0 30 => 11110 11的二进制? 11 % 2 = 1,5 5 % 2 = 1,2 2 % 2 = 0,1 1 % 2 = 1,0 11 => 1011 二进制如何转化为十进制? 101010 => ? 0*(2**(1-1))+1*(2**(2-1))+0*(2**(3-1))+1*(2**(4-1))+0*(2**(5-1))+1*(2**(6-1)) = 0 + 2 + 0 + 8 + 0 + 32 = 42 111111 => ? 1*(2**(1-1))+1*(2**(2-1))+1*(2**(3-1))+1*(2**(4-1))+1*(2**(5-1))+1*(2**(6-1)) = 1 + 2 + 4 + 8 + 16 + 32 = 63 """ a, b = 30, 11 # bin(整型) => 二进制 print(bin(a)) print(bin(b)) # int(二进制) => 十进制 print(int(0b101010)) print(int(0b111111)) # 11110 # 01011 # 1.按位与:&,规则:对应的两位为1则为1,否则为0 print(bin(a & b), a & b) # 01010 => 1*(2**(2-1))+ 1*(2**(4-1)) = 2 + 8 = 10 # 2.按位或:|,规则:对应的两位有一位为1则为1,否则为0 print(bin(a | b), a | b) # 11111 => 2**0 + 2**1 + 2**2 + 2**3 + 2**4 = 1 + 2 + 4 + 8 + 16 = 31 # 3.按位异或:^,规则:对应的两位不相同则为1,否则为0 print(bin(a ^ b), a ^ b) # 10101 => 2**0 + 2**2 + 2**5 = 1 + 4 + 16 = 21 # 4.按位取反:~,规则:所有正整数的按位取反是本身+1的负数,所有的负数按位取反是其本身+1的绝对值,0的按位取反是-1 print(~a) # -31 print(~b) # -12 print(~0) # -1 print(~-10) # 9 # 5.左移:<<,规则:全部二进制位左移若干位 print(bin(a << 2), a << 2) # 1111000 => 2**3 + 2**4 + 2**5 + 2**6 = 8 + 16 + 32 + 64 = 120 # 6.右移:>>,规则:全部二进制位右移若干位 print(bin(a >> 2), a >> 2) # 111 => 2**0 + 2**1 + 2**2 = 1 + 2 + 4 = 7
""" 逻辑运算符 and 逻辑与 同为True则为True,否则为False or 逻辑或 一个True则为True,否则为False not 逻辑非 非真即假,非假即真 """ print(True and True) # True print(True and False) # False print(False and False) # False print(True or True) # True print(True or False) # True print(False or False) # False print(not True) # False print(not False) # True
成员运算符 判断元素是否存在于某一个对象中 in 存在于对象中返回True,否则返回False not in 不存在于对象中返回True,否则返回False """ a = [1, 2, 3, 4, 5] b = ('a', 'b', 'c', 'd', 'e') c = {"苹果", "香蕉", "雪梨", "西瓜"} d = "good good study python" print(1 in a) # True print(10 not in a) # True print('f' in b) # False print("c" not in b) # False print("苹果" in c) # True print("葡萄" not in c) # True print("python" in d) # True print("java" not in d) # True
""" 身份运算符 is 判断两个标识符是否引用的是同一个对象(内存地址相同) is not 判断两个标识符是否引用的是不同的对象(内存地址不同) id(对象) => 内存地址 """ a, b = 10, 10 c, d = "python", "java" print(id(a), id(b)) print(id(c), id(d)) print(a is b) # True print(a is not b) # False print(c is d) # False print(c is not d) # True
# 运算符优先级 print(~3 ** 2) # ~9 => -10 print(3 * -3) # -9 print(1 + 2 * 3) # 7 print(3 << 1 + 1) # 3 << 2 => 0b11 << 2 => 0b1100 => 12 print(9 & 3 << 2) # 9 & 12 => 0b1001 & 0b1100 => 0b1000 => 8 print(10 ^ 9 & 12) # 10 ^ 8 => 0b1010 & 0b1000 => 0b10 => 2 print(1 <= 10 ^ 8) # True print(1 + False) # 1 print(1 in [3, 2, 1] is True) # False print(not 1 not in [3, 2, 1]) # True
posted on 2019-10-08 16:59 zhangmingda 阅读(663) 评论(0) 编辑 收藏 举报