Last modified: Mar 28, 2026 By Alexander Williams
Python For Loop: Sum Numbers in a Range
Adding numbers is a fundamental task in programming. Python makes it easy. You can sum a sequence of numbers using a for loop. This method is perfect for beginners.
It teaches core concepts like loops and variables. This guide will show you how. We will use the range() function to create our sequence.
Understanding the Python For Loop
A for loop repeats a block of code. It iterates over a sequence. This sequence can be a list, a string, or a range of numbers.
The loop runs once for each item in the sequence. It is a powerful tool for automation. You avoid writing the same code many times.
For summing numbers, we let the loop visit each number. It then adds each number to a running total.
The Role of the Range Function
The range() function is key here. It generates a sequence of numbers. You can learn more about its flexibility in our Python Range Function Guide: Syntax & Examples.
You can specify a start, stop, and step value. For a simple count from 0 to n, use range(n). The loop will handle the rest.
It does not create a list in memory. This makes it efficient for large ranges. It produces numbers one by one as needed.
Step-by-Step: Building the Summation Program
Let's build the program step by step. We will sum all numbers from 1 to 10.
First, we need a variable to hold the total. We often call it total or sum. Initialize it to 0.
# Step 1: Initialize the sum variable
total = 0
Next, we create the for loop. We use range(1, 11). This gives us numbers from 1 to 10. The stop value (11) is not included.
# Step 2: Create the for loop with range
for number in range(1, 11):
# Step 3: Add the current number to the total
total = total + number
Inside the loop, we update the total. The line total = total + number adds the current number. This happens in each iteration.
Finally, we print the result.
# Step 4: Print the final sum
print(f"The sum of numbers from 1 to 10 is: {total}")
Complete Code Example and Output
Here is the complete script. It puts all the steps together.
# Program to sum numbers from 1 to 10 using a for loop
# Initialize accumulator variable
total_sum = 0
# Loop through each number in the range 1 to 10
for num in range(1, 11):
# Add the current number to the running total
total_sum += num # This is shorthand for total_sum = total_sum + num
# Display the final result
print("The sum of numbers from 1 to 10 is:", total_sum)
When you run this code, you will see the following output.
The sum of numbers from 1 to 10 is: 55
Customizing Your Range
You are not limited to 1 to 10. The range() function is versatile. You can sum numbers between any two values.
For example, to sum numbers from 5 to 20, use range(5, 21). Remember, the stop value is exclusive.
# Sum numbers from 5 to 20
sum_custom = 0
for i in range(5, 21):
sum_custom += i
print(f"Sum from 5 to 20: {sum_custom}")
Sum from 5 to 20: 200
You can also sum only even numbers. Use a step value of 2. Start from an even number like 2.
# Sum even numbers from 2 to 30
even_sum = 0
for even_num in range(2, 31, 2):
even_sum += even_num
print(f"Sum of even numbers (2-30): {even_sum}")
Sum of even numbers (2-30): 240
Why Use a For Loop for This Task?
Using a for loop is an excellent learning exercise. It reinforces how loops work with sequences. You see how a variable's state changes over time.
It is also very readable. The code clearly shows the intention: "for each number in this range, add it to the total."
While Python has a built-in sum() function, understanding the loop is crucial. It builds a foundation for more complex problems. For instance, you might need to add only numbers that meet a certain condition.
Common Mistakes to Avoid
Beginners often make simple errors. Here are two common ones.
Forgetting to initialize the sum variable. If you don't set it to 0, Python will throw an error. The variable total won't exist when you try to add to it.
Using the wrong stop value in range. To include the number 10, you must use range(1, 11). Using range(1, 10) will only go up to 9. Always remember the stop is exclusive.
Another tip is to use the augmented assignment operator +=. It makes the code cleaner. total += number is the same as total = total + number.
Expanding the Concept: Conditional Summation
Once you master the basic loop, you can add logic. Use an if statement inside the loop. This lets you sum only specific numbers.
Let's sum only numbers divisible by 3 between 1 and 50.
# Sum numbers divisible by 3 from 1 to 50
conditional_sum = 0
for value in range(1, 51):
if value % 3 == 0: # Check if the number is divisible by 3
conditional_sum += value
print(f"Sum of numbers divisible by 3 (1-50): {conditional_sum}")
Sum of numbers divisible by 3 (1-50): 408
This pattern is powerful. You can filter for any condition. This is where the for loop shines over a simple sum() call.
Conclusion
Adding numbers in a range with a Python for loop is a core skill. It combines loop mechanics with the useful range() function. You start with a total variable set to zero.
You then loop through each number provided by range(). You add each number to the total. Finally, you print the result.
This process teaches variable initialization, iteration, and accumulation. It is a building block for more advanced data processing. Practice with different ranges and conditions to become comfortable.
Remember, the key is understanding the flow. The loop visits each number once. It performs your specified action. Mastering this opens the door to solving many programming problems. For more details on creating sequences, revisit our guide on the Python Range Function.