Tags
python
Asked 3 years ago
5 Aug 2021
Views 351
Eusebio

Eusebio posted

Is the following Python code valid : checking value by == ( Double Equal )?

Is the following Python code valid ?

a =1
b="1"
print(a==b)


what is wrong in above code , because it should be print true but it pring false instead
jaman

jaman
answered Aug 6 '21 00:00

a==b return False because a is not equal to b variable, both same value but have a different type.
a=1, variable a has an integer value of 1.
b="1" so b has string value because you enclosed with "1" double quotation so it interpreted as string
so in Python, == operator checks the type of variable and value of the variable if both type and value of variable are equal then only it runs true or it returns false.
jagdish

jagdish
answered Aug 6 '21 00:00

either try this code where b=1 , integer 1 value

a =1
b=1
print(a==b)

or a and b both have string value 1 :

a ="1"
b="1"
print(a==b)


both above code return True ,
Post Answer