How to do sum in Loop
<p>I have a exercise ,Input data will contain the total count of pairs to process in the first line.
The following lines will contain pairs themselves - one pair at each line.
Answer should contain the results separated by spaces.
My code:</p>

<pre><code>    n = int(raw_input())
    sum = 0

    for i in range(n):
        y = raw_input().split(" ")
        for i in y:
            sum = sum + int(i)


    print sum          
</code></pre>

<p>With my code , I come the Sum together, but I will that the results to come separated by spaces . Thanks for yours help .</p>

<p>with your current code what you get is the total sum of all the given numbers, to get the sum per line you need to initialize your counter in the outer loop, and then print it, and as you want to print all it in the same line there are several ways to do it, like save it in a list or telling <a href="https://docs.python.org/2/reference/simple_stmts.html#print" rel="nofollow">print</a> that don't print a new, line which is done by adding a <code>,</code> at the end like <code>print x,</code> with that in mind then the changes needed are</p>

<pre><code>n = int(raw_input())
for i in range(n):
    pairs = raw_input().split() #by default split use spaces 
    pair_sum = 0
    for p in pairs:
        pair_sum += int(p) # a += b is the same as a = a + b
    print pair_sum, 

 print ""  # to print a new line so any future print is not done in the same line as the previous one
</code></pre>

<p>that was the version with print per line, next is the version using list</p>

<pre><code>n = int(raw_input())
resul_per_line = []
for i in range(n):
    pairs = raw_input().split() #by default split use spaces 
    pair_sum = 0
    for p in pairs:
        pair_sum += int(p) # a += b is the same as a = a + b
    resul_per_line.append( str(pair_sum) ) #conver each number to a string to use with join bellow

 print " ".join(resul_per_line) 
</code></pre>

<p>with either of the above let said for example that the input data is</p>

<pre><code>3
1 2
40 50
600 700
</code></pre>

<p>then the result would be</p>

<pre><code>3 90 1300
</code></pre>

<hr>

<p>some parts of the above code can be simplify by using <a href="https://docs.python.org/2/library/functions.html#print" rel="nofollow">built in</a> functions like <a href="https://docs.python.org/2/library/functions.html#map" rel="nofollow"><code>map</code></a> and <a href="https://docs.python.org/2/library/functions.html#sum" rel="nofollow"><code>sum</code></a>, for example this part</p>

<pre><code>    pair_sum = 0
    for p in pairs:
        pair_sum += int(p)
</code></pre>

<p>can become</p>

<pre><code>    pair_sum = sum( map(int,pairs) )      
</code></pre>

python loops sum