0% found this document useful (0 votes)
10 views5 pages

Python Sum with For and While Loops

Uploaded by

roymandesh3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Python Sum with For and While Loops

Uploaded by

roymandesh3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 A simple Python program using a for loop.

Output:
The sum of numbers from 1 to 10 is: 55
# Initialize a variable to store the sum
total_sum = 0

# Loop through the numbers from 1 to 10 (inclusive)


for number in range(1, 11):
total_sum += number # Add the current number to total_sum

# Print the final sum


print("The sum of numbers from 1 to 10 is:", total_sum)
 Initialize total_sum:
Before the loop starts, we create a variable total_sum and set it to 0. This variable will be used to store the cumulative sum of the numbers.

 Loop with for:


The for loop in Python is used to iterate over a sequence of values. The range(1, 11) function generates numbers from 1
to 10. The loop will run 10 times, once for each number in that range.

 Add numbers:
During each iteration of the loop, the current value of number is added to total_sum. This step accumulates the sum of the numbers.

 Print the result:


After the loop finishes (i.e., after all numbers from 1 to 10 have been added), the final value of total_sum is printed.
 a simple Python program using a while loop to calculate the sum of numbers from 1 to 10:

# Initialize variables
total_sum = 0
number = 1 Output:

The sum of numbers from 1


# While loop to calculate the sum of numbers from 1 to 10 to 10 is: 55
while number <= 10:
total_sum += number # Add the current number to total_sum
number += 1 # Increment the number by 1

# Print the final sum


print("The sum of numbers from 1 to 10 is:", total_sum)
Explanation:

1. Initialize Variables:
 total_sum is initialized to 0 to store the cumulative sum of the numbers.
 number is initialized to 1, which will be the starting point of our loop

[Link] loop condition

The loop will continue as long as number is less than or equal to 10 (number <= 10). The moment this
condition is no longer true (when number becomes 11), the loop will stop.

3. Add Numbers:
Inside the loop:
 We add the current value of number to total_sum.
 Then, we increment the value of number by 1 (number += 1) to move to the next number in the sequence.

4. End of Loop:
When number becomes 11, the condition number <= 10 fails, so the loop exits, and the final sum is printed.
 The while loop is useful when the number of iterations is not known in advance. It
will keep executing as long as the specfied condition is true.
 In this case, the loop continues until the value of number exceeds 10.

You might also like