返回列表 發帖

for 迴圈 (二)

假設有一串列其內容如下:
list=["忠","孝","仁","愛","信","義","和","平"]

試以for迴圈,橫向顯示串列中的所有元素:

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")
複製代碼
  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for i in range(0,8):
  3.     print(list[i],end=" ")
複製代碼
橫向顯示串列中的部分元素:

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list[2:5]:
  3.     print(s,end=" ")
複製代碼
  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list[4:1:-1]:
  3.     print(s,end=" ")
複製代碼

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")

  4. list=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(0,8):
  6.     print(list[i],end=" ")
  7.    
  8. list=["忠","孝","仁","愛","信","義","和","平"]
  9. for s in list[2:5]:
  10.     print(s,end=" ")
  11.    
  12. list=["忠","孝","仁","愛","信","義","和","平"]
  13. for s in list[4:1:-1]:
  14.     print(s,end=" ")
複製代碼

TOP

  1. list1=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list1:
  3.     print(s,end=" ")
  4. list2=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(5,8):
  6.     print(list2[i],end=" ")
複製代碼
回復 1# tonyh

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")

  4. list=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(0,8):
  6.     print(list[i],end=" ")
  7.    
  8. list=["忠","孝","仁","愛","信","義","和","平"]
  9. for s in list[2:5]:
  10.     print(s,end=" ")
  11.    
  12. list=["忠","孝","仁","愛","信","義","和","平"]
  13. for s in list[4:1:-1]:
  14.     print(s,end=" ")
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for i in range(0,8):
  3.     print(list[i],end=" ")
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")

  4. list=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(0,8):
  6.     print(list[i],end=" ")
  7.    
  8. list=["忠","孝","仁","愛","信","義","和","平"]
  9. for s in list[2:5]:
  10.     print(s,end=" ")
  11.    
  12. list=["忠","孝","仁","愛","信","義","和","平"]
  13. for s in list[4:1:-1]:
  14.     print(s,end=" ")
複製代碼

TOP

  1. list=(1,2,3,4,5,6,7,)
  2. for i in list:
  3.     print (i)
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")
  4. list=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(0,8):
  6.     print(list[i],end=" ")
  7. list=["忠","孝","仁","愛","信","義","和","平"]
  8. for s in list[2:5]:
  9.     print(s,end=" ")
  10. list=["忠","孝","仁","愛","信","義","和","平"]
  11. for s in list[4:1:-1]:
  12.     print(s,end=" ")
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. for s in list:
  3.     print(s,end=" ")
  4. list=["忠","孝","仁","愛","信","義","和","平"]
  5. for i in range(0,8):
  6.     print(list[i],end=" ")
  7. list=["忠","孝","仁","愛","信","義","和","平"]
  8. for s in list[2:5]:
  9.     print(s,end=" ")
  10. list=["忠","孝","仁","愛","信","義","和","平"]
  11. for s in list[4:1:-1]:
  12.     print(s,end=" ")
複製代碼

TOP

返回列表