!= operator | Python Language Forum
H
Heena Goyal Posted on 29/07/2023
x = 5
if x != 1:
    print("1- x is actually 1")
elif x == 1:
    print(" 2= x is actually 2")
elif x == 1:
    print(" x is actually 3")
elif x == 4:
    print(" x is actually 4")
else:
    print(" x is actually 5")

Didn't understand the statements in this code, we did in class.

Here, when we say x is not equal to 1 then how come we say x is actually 1 in statement and likewise in statements below.

Y
Yogesh Chawla Replied on 30/07/2023

x = 5
if x != 1: #when first if condition gets satified then flow never goes to following elif
    # this is how if...elif...else conditions are checked by parser
    #this is how if...else happens in all languages
    print("1 - x is not equal to 1")
elif x == 1:
    print("2 = x is actually 1")
elif x == 2:
    print(" x is actually 2")
elif x == 3:
    print(" x is actually 3")
elif x == 5:
    print(" x is actually 5")
else:
    print(" x is neither -1,1,2,3")

#Didn't understand the statements in this code, we did in class.

#Here, when we say x is not equal to 1 then how come we say x is actually 1 in statement and
# likewise in statements below.


#but if your code is like this
x = 5
if x == 1: #here first if condition did not get satified that is the reason
    # flow reaches to following elif and it worked at fourth elif
    print("1 - x is not equal to 1")
elif x == 2:
    print("2 = x is actually 2")
elif x == 3:
    print(" x is actually 3")
elif x == 4:
    print(" x is actually 4")
elif x == 5:
    print(" x is actually 5")
else:
    print(" x is neither -1,2,3,4 or 5")


H
Heena Goyal Replied on 30/07/2023

Still did not get this, can we take this in the class?