1、列印九九乘法表
#只列印結果
for i in range(1,10):
for j in range(1,i+1):
print(i*j,end=" ")
print()
#列印算數表示式
for i in range(1,10):
for j in range(1,i+1):
print("* = ".format(j,i,i*j),end=" ")
print()
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81
#將列印矩陣轉置一下
for i in range(1,10):
print(" " *10*(i-1), end = " ")
for j in range(i,10):
print("* = ".format(i,j,i*j),end=" ")
print()
1*1 = 1 1*2 = 2 1*3 = 3 1*4 = 4 1*5 = 5 1*6 = 6 1*7 = 7 1*8 = 8 1*9 = 9
2*2 = 4 2*3 = 6 2*4 = 8 2*5 = 10 2*6 = 12 2*7 = 14 2*8 = 16 2*9 = 18
3*3 = 9 3*4 = 12 3*5 = 15 3*6 = 18 3*7 = 21 3*8 = 24 3*9 = 27
4*4 = 16 4*5 = 20 4*6 = 24 4*7 = 28 4*8 = 32 4*9 = 36
5*5 = 25 5*6 = 30 5*7 = 35 5*8 = 40 5*9 = 45
6*6 = 36 6*7 = 42 6*8 = 48 6*9 = 54
7*7 = 49 7*8 = 56 7*9 = 63
8*8 = 64 8*9 = 72
9*9 = 81
2、列印乙個菱形(行數為奇數行)
for i in range(-3,4):
prespace=(-i if i<0 else i)
print(" "*prespace + '*'*(7-2*prespace))
#變形之列印一道閃電
while true:
line_max = int(input("please input a odd number: "))
if line_max % 2:
break
range_num = (line_max + 1)//2
for i in range(-range_num+1,range_num):
if i == 0:
print('*'*line_max)
elif i > 0:
print(' '*(range_num-1) + "*"*(range_num-i))
else:
print(' '*(-i) + '*'*(range_num+i))
#變形之列印掏空的菱形
for i in range(-4,5):
if i == -4 or i == 4:
print('*'*9)
else:
prespace=(-i+1 if i<0 else i+1)
print("*"*prespace + ' '*(9-2*prespace) + "*"*prespace)
3、列印100以內的斐波那契數列
f0, f1 = 0, 1
while f1 <= 100:
print(f1, end = " ")
t = f1
f1 = t + f0
f0 = t
暫時還不理解的寫法:
f0, f1 = 0, 1
while f1 <= 100:
print(f1)
f0, f1 = f1, f0 + f1 #python對這樣的賦值怎樣處理
4、求斐波那契數列第101項
f0, f1 = 0, 1
for i in range(3,102):
t = f1
f1 = t + f0
f0 = t
else:
print(f1)
python語法練習 python語法練習
簡單的需求,獲取重複次數最多的資料 沛公import random dict for i in range 1,1000000 rd num random.randrange 0,999,1 if rd num in dict dict rd num dict rd num 1 else dict ...
Python基礎語法練習2
1.隨機生成5個 100 100之間 包括 100與100 的整數儲存到列表中,按絕對值從小到大排列 若有相同數字,相同數字並列排序即可 並列印輸出列表 import random num list random.randint 100 100 for i in range 5 列表推導式 new ...
Python語法練習8 異常
python使用被稱為異常的特殊物件來管理程式執行期間發生的錯誤。若編寫了處理異常的 程式將繼續執行,若沒有,程式將停止並顯示乙個traceback.2.try except 塊 1 若try 塊中的 執行起來沒有問題,python將跳過except 塊,若在太容易 塊中發生錯誤,python將查詢...