Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 4 < '3' return True in Python 2?

Tags:

Why does 4 < '3' return True in Python 2?

Is it because when I place single quotes around a number Python sees it as a string and strings are bigger than numbers?

like image 945
user1005318 Avatar asked Nov 01 '11 16:11

user1005318


People also ask

Why does Python not return 0 true?

By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. Here are most of the built-in objects considered false: Constants defined to be false: None and False.

Is True equal to 1 in Python?

Because True is equal to 1 and False is equal to 0 , adding Booleans together is a quick way to count the number of True values.

Why a B is true in Python?

This is because on the ASCII (American Standard Code For Information Interchange) CHART the letter "a" equates to 97 (in decimal values) while the letter "b" equates to 98 (in the decimal values).

What is the value of 3 4 in Python?

7 is the correct answer to the given question. In the given question the 3 will be represented as 011 in bits format ans 4 is represented as 100 as the bits format .


1 Answers

Yes, any number will be less than any string (including the empty string) in Python 2.

In Python 3, you can't make arbitrary comparisons. You'll get a TypeError.


From the link in eryksun's comment:

if (PyNumber_Check(v))     vname = ""; else     vname = v->ob_type->tp_name; if (PyNumber_Check(w))     wname = ""; else     wname = w->ob_type->tp_name; c = strcmp(vname, wname); 

So at least in recent versions of CPython 2.x, type names are compared, with an empty string used instead of the type name for any numeric type.

like image 154
agf Avatar answered Oct 01 '22 01:10

agf