The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

414373 (4) [Avatar] Offline
#1
Hi Ana,

May I know when is it appropriate to use "is None" in control statements?

Best Regards
Daniel
Ana Bell (33) [Avatar] Offline
#2
None is just a value for the special type NoneType. Compare this to something like 5 being a value for the int type. But what is special about None is that it is the only value for the NoneType type -- it has no other values! Whereas the int type has all the integers (0,1,2,3,1000,-4,-5,-6,etc).

So you can have a program that sets some variable to be None, to represent something special, like the absence of a value.

The statement is None just checks if the value of something literally is None. It is equivalent to checking if something == None. Not sure how far you are in the book, so I'll do something really simple. For example, say that you get a pet:

name_it = input("Do you want to name your dog? ")
if name_it == "yes":
    dog_name = "Ruff"
else:
    dog_name = None

# you can check equivalence using the keyword is
# the line below is equivalent to dog_name == None
if dog_name is None:
    print("Give your dog a name!")
else:
    print("Hello", dog_name)

414373 (4) [Avatar] Offline
#3
Thanks, its crystal clear now, and you gave a great example smilie None == Absence of anything.