一、1到100求和(三种方式)
1、一行代码实现:
a=sum(range(1,101))
print(a)
2、while实现
i = 1
sum = 0
while i <= 100:
sum += i
i +=1
print(sum)
3、for实现
sum = 0
for i in range(1, 101):
sum += i
print(sum)
二、求奇数和
for循环
1、sum = 0
for i in range(1, 101,2):
sum += i
print(sum)
while循环
2、i = 1
sum = 0
while i <= 100:
if i % 2 == 0:
sum += i
i += 1
print(sum)
三、求偶数和
for循环
1、sum = 0
for i in range(2, 101,2):
sum += i
print(sum)
while循环
2、i = 1
sum = 0
while i <= 100:
if i % 2 != 0:
sum += i
i += 1
print(sum)