标签:range str while 计算 nbsp code class 奇数 ==
1、计算1~100的数之和
----for循环实现1~100的和
sum1 = 0 for i in range(1,101): sum1 = sum1 + i i += 1 print(f"1-100之间的和是:{sum1}")
----while实现1~100的和
sum1 = 0 i = 1 while True: sum1 = sum1 + i if i == 100: break i += 1 print(sum1)
2、计算1~100里面偶数之和
sum1 = 0 for i in range(1,101): if i % 2 == 0: sum1 = sum1 +i i += 1 print(f"1-100之间偶数的和是:{sum1}")
3、计算1~100里面奇数之和
sum1 = 0 for i in range(1,101): if i % 2 != 0: sum1 = sum1 +i i += 1 print(f"1-100之间奇数的和是:{sum1}")
4、一条代码求1~100的和:使用sum函数
print(sum(range(1,101)))
python计算1~100的和,1~100奇数的和,1~100偶数的和,一条代码求1~100的和
标签:range str while 计算 nbsp code class 奇数 ==
原文地址:https://www.cnblogs.com/pychina/p/10235884.html