返回列表 發帖

for 迴圈 (一)


#for i in [list的資料型態]

#range(start=0,stop,step=1)

#range(1,6)->[1,2,3,4,5]
#range(5)->[0,1,2,3,4]
#range(1,5)->[1,2,3,4]
#range(1,5,2)->[1,3]


直向顯示第1名~第5名

  1. for i in range(1,6):
  2.     print("第",i,"名")
複製代碼

  1. for i in range(1,6): #同樣是到6的前面一位
  2.     print("第",i,"名")

  3. sum = 0
  4. for j in range(1,101):
  5.     sum +=j
  6. print(f"sum={sum}")
複製代碼

TOP

  1. for i in range(1,6):
  2.     print("第",i,"名")
複製代碼

TOP

  1. list=[1,2,4,8,16,32,64,128,256]
  2. for i in range(1,5):
  3.     print("第",i,"名")
複製代碼

TOP

  1. list=[1,2,4,8,16,32,64]
  2. for i in range(1,5):
  3.     print("第",i,"名")
複製代碼

TOP

本帖最後由 朱宥愷 於 2024-7-9 11:07 編輯
  1. range(0,100,3)
  2. print('到一百3的倍數')
  3. for i in range(0,100,3):
  4.     print(i)
複製代碼

TOP

  1. for i in range(1,100):
  2.     print("第",i,"名")
複製代碼

TOP

for i in range(1,3858,68):
    print("第",i,"名")

TOP

  1. for i in range(1,10):
  2.     print("第",i,"名")
複製代碼

TOP

  1. for i in range(1,101,1):
  2.     print("第",i,"名")
複製代碼

TOP

  1. list=[1,2,4,8,16,32,64,128]
  2. for i in range(1,6):
  3.     print("第",i,"名")
複製代碼

TOP

  1. for x in range(1,101,1):
  2.     print("第",x,"名")
複製代碼

TOP

返回列表