+ 2

Compare 4.0 and 4 in python

when I run the below program with for example 4 and 4.0 in sololearn app, gives 4.0 is greater than 4 why? x = input("enter fist number:") y = input("\nenter second number:") if x > y: print("\nfirst number is greater.") elif y > x: print("\nsecond number is greater.") else: print("\nnumbers are equal.") but when I define x and y in the code itself as x=4.0 and y=4 it prints numbers are equal https://code.sololearn.com/cVkL6Z4m1ijq/?ref=app

27th Oct 2017, 5:43 AM
Khatereh Azizi
3 Respuestas
+ 4
you are welcome @Azizi ☺
27th Oct 2017, 10:02 AM
Sri Lakshmi
Sri Lakshmi - avatar
+ 3
of course 4 is equal to 4.0 in terms of value, but in terms of datatype they are different. ..4 is integer & 4.0 is float hence the numbers are equal
27th Oct 2017, 9:40 AM
Sri Lakshmi
Sri Lakshmi - avatar
+ 3
Thank you @Sri . I got your point: When using input function, the code gives the values in string format, and '4.0' is grater than '4' But when we define x=4.0 and y=4 in the code itself, they are numers, and output will be: they are equal
27th Oct 2017, 9:51 AM
Khatereh Azizi