将数字从小到大排列, (用pycharm中的断点和Debugger来看程序运行的步骤)
def find_smallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1,len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index def selection_sort(arr): new_arr = [] for i in range(len(arr)): smallest = find_smallest(arr) new_arr.append(arr.pop(smallest)) return new_arr print(selection_sort([8,6,4,9,2]))
用pycharm中的断点和Debugger来看程序运行的步骤
for i in range (1,10): 九九乘法表 for j in range (1,10): print('{:2d}*{:2d}={:2d}'.format(i,j,i*j),end=' ') print()
i=1然后进入内循环,j=1到9;然后i=2再进入内循环穷举j=1到9,直到i=9进内循环穷举j=1到9
print函数默认换行,是end=' '在起作用,
python默认的print()是换行输出的
print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)
In [8]: for i in range(1,9): print(i) 1 2 3 4 5 6 7 8 In [9]: for i in range(1,9): print(i,end="") 12345678 In [11]: for i in range(9): print(i,end='$$$') 0$$$1$$$2$$$3$$$4$$$5$$$6$$$7$$$8$$$ In [12]: for i in range(9): print(i,end=' ') 0 1 2 3 4 5 6 7 8