jeudi 16 juillet 2020

Using append in an if statement

I don't get why the following two bits of code give different results:

x = [1,2,3]
y = [1,2,3,4]

if (x.append(4) == y) or (x == y):
    print(True, x, y)
else:
    print(False, x, y)

#prints True [1, 2, 3, 4] [1, 2, 3, 4]
x = [1,2,3]
y = [1,2,3,4]

if (x == y) or (x.append(4) == y):
    print(True, x, y)
else:
    print(False, x, y)

#prints False [1, 2, 3, 4] [1, 2, 3, 4]

Why does swapping the order of the two conditions give different results? x.append(4) makes x become [1,2,3,4], as is seen in either output. So it seems like the condition x.append(4) == y should be True in either case, satisfying the or of the if statement.

Aucun commentaire:

Enregistrer un commentaire