Home > Article > Backend Development > 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!