How to perform addition and division in python
Question: I want to get the sum of all numbers within a list. My code is shown below;
however, I am getting an error when I try to run it:

    
    
    c = [795557,757894,711411,556286,477322,426243,361643,350722]
    for c1 in c:
       x = x + c1
    

I am also trying to divide one number by another. However, the result is
always zero:

    
    
    y=(273591/21247633)*100
    


Answer: In the first case, you need to define `x` before you use it and use `c1`
instead of `c`:

    
    
    x = 0
    c=[795557,757894,711411,556286,477322,426243,361643,350722]
    for c1 in c:
        x=x+c1
    print x
    

You can try this code online [here](http://codepad.org/0C0nfi0A).

* * *

In the second case, you need to [use floating point
numbers](http://codepad.org/FfqIghfv) instead of integers:

    
    
    y=(273591/21247633.0)*100
    print y
    

This is because the result of an integer-integer division in Python 2.x is
_also_ an integer. In this case, `273591 ÷ 21247633 = 0.0128763048571` so the
result is rounded down to `0`.

[This has changed in Python 3.x](https://www.python.org/dev/peps/pep-0238/),
and you can enable the same behavior in Python 2.x as follows:

    
    
    from __future__ import division
    y=(273591/21247633)*100
    print y
    

