[Python] Is there a difference between "==" and "is"?

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    My Google-fu has failed me.

    In Python, are the following two tests for equality equivalent?

    n = 5
    # Test one.
    if n == 5:
    print 'Yay!'

    # Test two.
    if n is 5:
    print 'Yay!'


    Does this hold true for objects where you would be comparing instances (a list say)?

    Okay, so this kind of answers my question:

    L = []
    L.append(1)
    if L == [1]:
    print 'Yay!'
    # Holds true, but...

    if L is [1]:
    print 'Yay!'
    # Doesn't.


    So == tests value where is tests to see if they are the same object?

    Continue reading...

Compartilhe esta Página