python是個很棒、很簡潔的語言。博主突發奇想用一行程式碼寫九九乘法表(日常騷操作)
目錄
目標
能用一行程式碼寫陣列,爲什麼要那麼多行
a = [i for i in range(10)]
a = ['%d*%d=%d'%(i,j,i*j) for i in range(1,4) for j in range(1,3)]
a = [i for i in range(10) if i%2==0]
a = [i for i in range(10) if i%2==0 and i%3 == 0]
注意:if-else在for前面,只有if在for後面;反之不行,原因未知QAQ
a = [i if i%2==0 else -1 for i in range(10)]
a = [print('%d*%d=%d'%(i,j,i*j), end=' ') if j<i+1 else print() for i in range(1,10) for j in range(1,i+2)]