How to keep variable in for loop and others'conditional statement wouldn't change it
<p>when program executed the second <code>if</code> and change <code>P = 0</code>, it will execute the next <code>if</code>. I know it's wrong, but I curiously wonder how to hold variables(<code>P</code>) constant and don't impact other in the if statement.</p>

<pre><code>    """
    :type a: str
    :type b: str
    :rtype: str
    """
class Solution(object):

    def addBinary(self, a, b):
        c = ''
        P = 0
        for i,j in zip(range(len(a)-1, -1, -1),range(len(b)-1, -1, -1)):
            if a[i] + b[j] == '11':
                c = c + '0'
                P = 1

           if  a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 1:
               c = c + '0'
               P = 0
           if  a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 0:
               c = c + '1'
           if  a[i] + b[j] == '00' and P == 1:
               c = c + '1'
               P =0
           if  a[i] + b[j] == '00' and P == 0:
               c = c + '0'
        T = len(a) - len(b)
        if T &gt; 0:
            c = c + a[:T]
        else:
            c = c + b[:T]
        return c[::-1]
</code></pre>

<p>Don't you want to use <code>elif</code>?</p>

<pre><code>if  a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 1:
    c = c + '0'
    P = 0
elif  a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 0:
    c = c + '1'
elif  a[i] + b[j] == '00' and P == 1:
    c = c + '1'
    P =0
elif  a[i] + b[j] == '00' and P == 0:
</code></pre>

python if-statement condition