Home >Backend Development >Python Tutorial >python uses for statement to add 1 to 100

python uses for statement to add 1 to 100

silencement
silencementOriginal
2019-06-05 13:11:1716501browse

python uses for statement to add 1 to 100

Use python to add from 1 to 100, you can use a for loop statement.

Define a function

def sumStartToEnd(start,end):
    sum = 0
    for n in range(start,end+1,1):
        sum = sum + n
    return sum
print(sumStartToEnd(1,100))

Output result 5050

You can also use a while loop to achieve

def sum():
sum = 0
x=1
while x < 101:
sum = sum + x
x+=1
return sum
print(sum())

The result is the same 5050

For more python related technical articles, please visit python tutorial to learn.

The above is the detailed content of python uses for statement to add 1 to 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