Python練習#2-for和while迴圈

by Gemma

本周目標

熟悉Python的基礎語法與資料結構

任務

  • 學習Python基礎語法(變數、數據類型、條件語句、迴圈)
  • 使用 if 條件控制和 for 迴圈來做簡單的數學計算
  • 深入學習列表(List)、字典(Dictionary)等資料結構

專案練習

使用for和while迴圈進行簡單的加總運算

while迴圈

n=1
sum=0
while n<=10:
    sum=sum+n
    n+=1
print(sum)

for 迴圈

sum=0
for n in range(11):
    sum=sum+n
print(sum)

Output

55

You may also like

Leave a Comment