首页 > 代码库 > python 数据运算

python 数据运算

算数运算:

 技术分享

a=10b=20print(a+b) #相加print(a-b) #减法print(a*b) #乘法print(b/a) #除法print((b+2)%a) #求余数print(b**a)  #幂次方print(b//a)  #取整数#------------------------------------------------结果如下:30-102002.02102400000000002

 

比较运算:

 技术分享

赋值运算:

技术分享

逻辑运算:

 技术分享

成员运算:

 技术分享

身份运算:

 技术分享

位运算:

技术分享

 

计算表示的最小的单位为一个二进制位计算存储的最小的单位为一个二进制位(bit)8bit = byte (字节)1024byte = 1kbyte取八个二进制位(bit)。a = 60 二进制位,如下:128    64    32    16    8    4    2    1------------------------------0    0    1    1    1    1    0    0 = 60b = 13  二进制位,如下:128    64    32    16    8    4    2    1------------------------------0    0    0    0    1    1    0    1 = 13 a&b  按位于(and),两个同时为真的值是为结果,结果如下:128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 600    0    0    0    1    1    0    1 = 13-------------------------------0    0    0    0    1    1    0    0 = 12a|b  按位或(or),两个有一个为真时,结果为真,如下:128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 600    0    0    0    1    1    0    1 = 13-------------------------------0    0    1    1    1    1    0    1 = 61a^b  按位异(^),相同为0,不同为1,如下128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 600    0    0    0    1    1    0    1 = 13-------------------------------0    0    1    1    0    0    0    1 =    49~a 按位取反(~),所得结果减256,视为结果。128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 600    0    0    0    1    1    0    1 = 13-------------------------------1    1    0    0    0    0    1    1 =195 195 -256 = -61(结果)a<< 左移动运算符,a<<2 移动两个位。如下:128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 60-------------------------------1    1    1    1    0    0    0    0 = 240  <--移动两个位,后面不足补0.a>> 右移动运算符,a>>2 移动两个位。如下:128    64    32    16    8    4    2    1-------------------------------0    0    1    1    1    1    0    0 = 60-------------------------------0    0    0    0    1    1    1    1 = 15-->,右移两位,空出部分补0
a = 60  # 60 = 0011 1100b = 13  # 13 = 0000 1101c = 0c = a & b;  # 12 = 0000 1100print("Line 1 - Value of c is ", c)c = a | b;  # 61 = 0011 1101print("Line 2 - Value of c is ", c)c = a ^ b;  # 49 = 0011 0001print("Line 3 - Value of c is ", c)c = ~a;  # -61 = 1100 0011print("Line 4 - Value of c is ", c)c = a << 2;  # 240 = 1111 0000print("Line 5 - Value of c is ", c)c = a >> 2;  # 15 = 0000 1111print("Line 6 - Value of c is ", c)

结果:

Line 1 - Value of c is  12Line 2 - Value of c is  61Line 3 - Value of c is  49Line 4 - Value of c is  -61Line 5 - Value of c is  240Line 6 - Value of c is  15进程已结束,退出代码0

运算符优先级:

技术分享

更多内容可以访问:

http://www.runoob.com/python/python-operators.html

 

python 数据运算