用python輸出1到100的奇數、偶數、5的倍數、奇數和、偶數和、總和
阿新 • • 發佈:2021-08-09
list1 = []
list2 = []
list3 = []
list4 = []
list5 = []
for i in range(1, 101):
if i % 2 == 0:
list1.append(i)
else:
list2.append(i)
if i % 5 == 0:
list3.append(i)
print('1到100的偶數有:', list1, '\n偶數和為:', sum(list1))
print('1到100的奇數有:', list2, '\n奇數和為:', sum(list2))
print('5的倍數有:', list3, '\n總和:', sum(list1) + sum(list2))
1到100的偶數有: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
偶數和為: 2550
1到100的奇數有: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
奇數和為: 2500
5的倍數有: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
總和: 5050
本文來自部落格園,作者:夏夏夏天的西瓜,轉載請註明原文連結:https://www.cnblogs.com/qiang6313669/p/15117388.html