!=operator with different data types | Python Language Forum
H
Heena Goyal Posted on 29/07/2023
E=1
F=1.0
G="1"
if E!=F:
    print(E!=F)
if F!=G:
    print(F!=G)
if E!=G:
    print(E!=G)

The correct output is:
False
True
True

But I am only getting:
True
True

I have tried other cases as well, file is enclosed for reference.

I am using VS Code but don't know why am I not getting False for any of the statements in each case.

I don't know if I am doing sth incorrect or what.



Y
Yogesh Chawla Replied on 30/07/2023

You did not get first False because first if conditional expression is false so why will the flow go inside first if

Second and third if condition output is true, that's the reason, flow went inside and output came as true

If you write like this, you will get first output as well, E and F belong to different classes, class of int and class of float

E=1
F=1.0
G="1"
if E==F:
    print(E==F)
if F!=G:
    print(F!=G)
if E!=G:
    print(E!=G)