数值操作
>>> 5/3 #divide
1.6666666666666667>>> 5//2 #floor
2
>>> 5//3
1
>>> 5 % 4 #mod
1
>>> divmod(5,3) #divide and mod
(1, 2)
>>> num =5
>>> num+=1
>>> num
6
>>> num-=1
>>> num
5
>>> num*5
25
>>> num//=5
>>> num
1
>>> num=25
>>> num // =5
File "<stdin>", line 1
num // =5
^
SyntaxError: invalid syntax
>>> num //=5
>>> num
5
>>> num*=5
>>> num
25
>>> num/=5
>>> num
5.0
>>>
注意这两个红字体暂未搞懂
bitwise 二进制操作
0b0101
5
>>> def b(n):print('{:08b}'.format(n)) #0 代表以0补齐 8代表8位
...
>>> b(5)
00000101
>>> x,y =0x55,0xaa
>>> b(x)
01010101
>>> b(y)
10101010
>>> b(x|y)
11111111
>>> b(x&y)
00000000
>>> b(x^y)
11111111
>>> b(x^0xff)
10101010
>>> b(x<<4)
10101010000
>>> b(x>>4)
00000101
>>> b(~4)
-0000101
>>>
is is not with == 值和id的比较
>>> x,y=[5],[5]
>>> x==y
True
>>> x is y
False
>>> id(x)
140389403266296
>>> id(y)
140389403264640
>>> x=5
>>> y=5
>>> id(x)
9357536
>>> id(y)
9357536
>>> x==y
True
>>> x is y
True
>>>
数组的操作
>>> list = [1,2,3,4,5,6,7,8,9]
>>> list[0]
1
>>> list[1]
2
>>> list[9]
>>> list[0:5]
[1, 2, 3, 4, 5]
>>> range(1,10)
range(1, 10)
>>> for i in range(0,10):print(i)
...
0
1
2
3
4
5
6
7
8
9
>>> list[:]=range(100)
>>> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> list[27]
27
>>> list[27:42]
[27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]
>>> list[27:42:3] #从下表为arg0到arg1的前一个并略过arg3个元素每次
[27, 30, 33, 36, 39]
>>> list[27:43:3]
[27, 30, 33, 36, 39, 42]
>>> for i in list[27:43:3]:print(i)
...
27
30
33
36
39
42
>>> for i in list[27:43:3]:print(i)
...
27
30
33
36
39
42
>>> list[27:42:3]=(99,99,99,99,99) #替换相应的元素
>>> list[27:42:3]
[99, 99, 99, 99, 99]
运算 加括号来表达你的意图
>>> 5*25+14/2
132.0
>>> 5*(25+14)/2
97.5
>>>