# 1.for 循環
#列表
L1=[10,20,'ABC','python']
print(L1[0])
for i in L1:
print(i)
10
10
20
ABC
python
#range 函數,生成一個序列
for i in range(10): #10 代表stop ,取不到
print(i,end=' ')
0 1 2 3 4 5 6 7 8 9
for i in range(5,10): # 5 代表start,10代表stop
print(i,end=" ")
5 6 7 8 9
# 5 代表start,10代表stop,2代表sep
for i in range(5,10,2):
print(i)
5
7
9
# 計算1+2+。。。+100
#寫法一
sum=0
for i in range(1,101,1):
sum=sum+i
print("1到100之和:",sum)
1到100之和: 5050
# 計算1+2+。。。+100
# 寫法二:
sum=0
for i in range(101):
sum+=i
print("1到100之和:",sum)
1到100之和: 5050
#2.while 循環
# 計算1+2+。。。+100
i=1;
sum=0
while i<=100:
sum=sum+i
i=i+1
print("1到100之和:",sum)
1到100之和: 5050