Home python None python value

None python value

Author

Date

Category

Why does it work like this, because None works for equivalence like null , and there is no value in a .

According to the idea, it should turn out to be True because the value None has nothing or is equal to 0 or False , but for some reason when entering such data into the variable a returns False Could you explain why this is happening.

& gt; & gt; & gt; a = ""
& gt; & gt; & gt; a == None
False
& gt; & gt; & gt; a = "0"
& gt; & gt; & gt; a == None
False
& gt; & gt; & gt; a = 0
& gt; & gt; & gt; a == None
False
& gt; & gt; & gt;

Answer 1, authority 100%

a you have this name. Using the = operator you attach this name to different objects (empty string '' , string with character '0' (U + 0030), integer number 0 – objects are created by the corresponding constants in the source code). Strings are objects of type str in Python. An integer is an int object.

a == None compares these objects to a None object. No object of type str or int can be equal to None. As an aside, None is also a Python object (of type NoneType). It exists in a single copy in the program, so it should be compared using the is operator: a is None (is the same object or not. Purpose == compare values ​​objects).

You were probably interested in the concept of “truthiness” in Python (meaning in a boolean context) and a really empty string (like other empty containers) and 0 integer are Falsey in Python:

if not a:
  print (f '{a! r} is Falsey')

See Truth Value Testing .

Note '0' is not an empty string, so bool ('0') is True . Python is a strongly typed language. String to a number or to None, the object will not implicitly turn into . The names themselves, such as a , can refer to any object at different times. But the name must refer to some object, otherwise you will get NameError .


Answer 2, authority 100%

In the first case, the a variable contains an empty string. The second line contains a zero character. In the third, the number 0. In all three cases, there is something in the variable.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions