python uses for statement to add 1 to 100

silencement
Release: 2019-06-05 13:11:17
Original
16399 people have browsed it

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))
Copy after login

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())
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!