Fundamentals 5 min read

5 Clever Python Ways to Compute 1‑2+3‑4+…+99

This article presents a Python fan's arithmetic challenge—calculating the alternating sum 1‑2+3‑4+…+99—and walks through five distinct code solutions, from basic loops to concise itertools one‑liners, highlighting their logic and trade‑offs.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
5 Clever Python Ways to Compute 1‑2+3‑4+…+99

Introduction

In a Python community a fan named dcpeng asked how to compute the alternating sum 1‑2+3‑4+…+99 using a loop that accumulates the result in a single variable.

The problem can be solved with simple arithmetic, but the focus is on implementing it correctly in Python.

Solutions

Method 1: dcpeng’s answer

odd = 0
even = 0
for i in range(100):
    if i % 2 == 1:
        odd += i
    else:
        even += i
print(odd - even)

This works but uses two variables, which deviates from the requirement of a single accumulator.

Method 2: dcpeng’s refined answer

count = 1
sum = 0
while count <= 99:
    if count % 2 == 1:
        sum += count
    else:
        sum -= count
    count += 1
print(sum)

This version follows the single‑variable rule and produces the correct result.

Method 3: Eternal from Budapest

Uses the range() function in a straightforward loop.

s = 0
for i in range(1, 100):
    if i % 2 == 0:
        s -= i
    else:
        s += i
print(s)

Method 4: Moon God

A compact two‑line solution leveraging itertools.accumulate.

from itertools import accumulate
list(accumulate((i if i % 2 else -i for i in range(1, 100))))

It can be simplified further with sum:

from itertools import accumulate
print(sum(accumulate((i if i % 2 else -i for i in range(1, 100)))))

Method 5: Yuliang teacher

A one‑liner that directly uses sum with a generator expression.

print(sum(i if i % 2 else -i for i in range(1, 100)))

Conclusion

The article collected five Python implementations for the alternating sum problem, ranging from explicit loops with separate variables to elegant one‑liners using itertools.accumulate and sum. Each solution demonstrates different Pythonic techniques and helps learners deepen their understanding of loops, conditionals, and functional tools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmPythonCode ExampleLoopsitertoolsalternating-sum
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.