Alternate between add and subtract on a list of numbers
<p>How to get the sum of the series 1-2+3-4+5-6+7-8+9-10 in python? </p>

<p>I know how to get the sum of numbers from 1 to a particular number by using while loop in this way:</p>

<pre><code>i = 1
sum = 0
while i&lt;= 10:
    sum = sum + i
    i = i + 1
print(sum)
</code></pre>

<p>your code need a very small change:</p>

<pre><code>i = 1
sum = 0
sign = 1
while i&lt;= 10:
    sum = sum + sign * i
    sign = sign * -1
    i = i + 1
print(sum)
</code></pre>

<p>There are more pythonic ways to do what you requested - but they will require a little more knowledge..</p>

python list