I'm reading an online book about Python,and the writer mentioned that "the None value can be helpful when you need to store something that won’t be confused for a real value in a variable", but I didn't understand it.Can anybody explain that for me with an example?
Asked
Active
Viewed 203 times
1 Answers
1
Let's say you have a program that reads a temperature from a sensor every minute, and stores them in a list. You might end up with a list like this:
[23, 24, 24, 23, 25, 24]
Now let's assume that sometimes the connection to your sensor fails (maybe it's wireless), and you get no reading. What do you put in your list? If you leave it out, you won't have an even 1-minute spacing between values any more. If you store a 'special' number (e.g. 0 or -1 or something) you'll have to know that the particular 'special' value means 'no measurement'. Using the None value for that is pretty handy, because it makes it clear that there is no value there. E.g. something like this:
[23, 24, None, 23, None, 24]

AaronI
- 842
- 6
- 12