[Python] Why does Python comparison syntax behave this way? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I ran into a strange issue with boolean logic while handling different inputs for a simple "yes/no" question.

    response = input('Use current settings Y/n? ')


    If I respond with y in the console, then the following print() commands output:

    print(response) # prints 'y' to the console
    print(response != 'y' or 'yes') # prints 'yes' to the console
    print(response == 'y' or 'yes') # prints 'True' to the console


    The first and last statement behave as I expected, but the second print() I expected to output False. Does anyone know what is going on or why this is?

    Continue reading...

Compartilhe esta Página