Home  >  Article  >  Backend Development  >  How to sum even numbers in python1-100?

How to sum even numbers in python1-100?

coldplay.xixi
coldplay.xixiOriginal
2020-06-22 13:22:3556565browse

How to sum even numbers in python1-100?

Recommended tutorial: "Python Video Tutorial"

How to sum even numbers in python1-100?

Python method of summing even numbers:

#求100内偶数while\for..in循环
sum = 0
i = 0
while i <=100:
    sum += i
    i += 2
print(sum)
 
 
sum = 0
#range函数end时不包括当前数值,所有注意101.
for i in range(0,101,2):
    sum += i
print(sum)

Result:

The sum of even numbers between 1-100 is: 2550

Recommended related articles: "python tutorial"

The above is the detailed content of How to sum even numbers in python1-100?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn