dimanche 2 février 2020

if is in python not working on negative number [duplicate]

I've been working in python for a while now but just now I've encountered an error that I can't explain.

def b():
    return -6
a = b()
if a is -6:
    print("Hi")

Whenever I run this I don't get into the if function and nothing gets printed. but if I make the number bigger than -6 meaning -5, -4 and etc this works properly.

def b():
    return -5
a = b()
if a is -5:
    print("Hi")

can someone explain to me why is this happening? the == operator work as it should but if is it not.

Aucun commentaire:

Enregistrer un commentaire