You can use loops to sum all integers from 1 to 100. Specifically, we can use for
Loop through all integers from 1 to 100, and add them to get the total.
For example:
sum = zero
for i in range ( one , one hundred and one ): sum += i print ( sum )
In the above code, we first initialize the variable sum
Is zero, then use for
Loop through all integers from 1 to 100 (including 1 and 100), and accumulate them to sum
Medium. Finally, use the print()
Function output sum
Is the sum of all integers from 1 to 100.
Note that built-in functions are used here range(start, stop[, step])
To generate a sequence containing all integers in the specified range. among start
Represents the starting number, stop
Represents the ending number (excluding this number), step
Indicates the step size. If omitted step
Parameter, the default value is 1. Therefore, in the above code, range(1, 101)
Represents all integers from 1 to 100, inclusive.