+ 3

None and 0

what is the difference between None and 0 print (0==None) output: False why?

24th Jul 2018, 8:59 AM
tardigrade
tardigrade - avatar
4 ответов
+ 2
Because None means NoneType and 0 is int type, internally as I saw in some docs, python sort types alphabetically, and uppercase go before lowercase, N > i, but here are some discrepancies what is sure is that None is not the same as 0 Hope it helps you Can any Python expert tell us how is Python sorting types internally?
24th Jul 2018, 9:15 AM
Guillem Padilla
Guillem Padilla - avatar
+ 1
None is nonetype class object. Here is my observation by printing. basically comparison operator compare same datatype so interpreter internally doing some implicit conversion and that may have two possibilities: 1. interpreter convert 0 to character and compare 2. interpreter convert None's first charcter to decimal and compare I have checked using code and comparison is False in both the case. here is my code: print(chr(0)) #print nothing as charcter zero is not printable character print(chr(78)) #print N as 78's ascii value is N print(ord('0')) #print 48 is decimal value of 0 print(ord('N')) #print 78 true cases: print(chr(78) == "N") print(78 == ord("N"))
24th Jul 2018, 11:27 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
Bro I'll try to keep it as simple as I can... NONE, is a special literal(it is classified differently) and 0 is an integer value i.e. numeric literal. You can't compare two different type of literals in python...or anywhere else. IF you are confused due to words like literals then think of it like this... Comparing 0 (numeric literal)and None(special literal) is like comparing "Hello" with 12. HOPE YOU GOT IT BUT IF YOU DIDN'T THEN JUST REPLY... I'M HERE TO HELP Happy Learning!
24th Aug 2018, 4:20 PM
Aatib Ahmed
Aatib Ahmed - avatar
0
Because the integer 0 is not equal to something that represents <having no value> (that is what None stands for). But I understand your confusion, because in the tutorial it says that None would be interpreted as False when used in Boolean expressions. But then some of the explanations given in this course are not sufficiently clear/ specific. For that you have to consult the Python language reference. Maybe this also makes it clearer. Only when you explicitely cast to boolean with bool() (example nr. 5 below) you do get the result you may have expected: #True print("1. " + str(None == None)) #False print("2. " + str(0 == None)) #True print("3. " + str(0 != None)) #False print("4. " + str(bool(0) == None)) #True print("5. " + str(bool(0) == bool(None)))
21st Oct 2020, 6:34 PM
CMW