21, the Python list can be used in the Swap method
Mylist[i], mylist[j] = Mylist[j], mylist[i]
So we can swap two elements.
And also
A ^= b
b ^= A
A ^= b
22. Python Rounding and keeping decimals
#encoding: utf-8
import math
#向上取整
print Math.ceil (2.6)
3.0
#向下取整
print Math.floor (2.6)
2.0
#四舍五入
print round (2.6)
3.0
#这三个的返回结果都是浮点型
#将a保留到小数点后n位
Round (A, N)
Print (3.1415926, 3) #
3.142
23, Python copy (deep copy deepcopy and light copy copy)
Objects in Python are assigned by reference, and if you need to copy objects, you need to use the Copy module in the standard library.
A, copy.copy a shallow copy copies only the parent object, does not copy the object's inner child objects.
B, Copy.deepcopy deep Copy object and its child objects
>>> import copy >>> a = [1,2,3,4,[' A ', ' B ']] #原始对象 >>> b = A #赋值, passing objects References >>> C = copy.copy (a) >>> d = copy.deepcopy (a) >>> a.append (5) >>> a[4].append (' C ') >>> print ' a= ', a a= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5] >>> print ' b= ', b b= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5] >>> print ' c= ', c c= [1, 2, 3, 4, [' A ', ' B ', ' C ']] >>> print ' d= ', d d= [1, 2, 3, 4, [' A ', ' B ']]