In python is "key not in container" allowed, or must we write "not (key in container)?" -


in python, know permissible write:

happy_bag = list()  if not (key in happy_bag):     print(key, " ain't in da bag.") 

but okay write:

happy_bag = list()  if key not in happy_bag:     print(key, " ain't in da bag.") 

also, following legal:

if key in happy_bag:     print("congratulations! have ", key, " in bag!") 

but alright if add word "is"?

if key in happy_bag:     print("congratulations! have ", key, " in bag!") 

it correct write:

container = [] key = 1 if key not in container:     print("not found") 

and advised. pep 8: programming conventions

use is not operator rather not ... is. while both expressions functionally identical, former more readable , preferred.

regarding second question is in not correct operator in python. operator is used test reference identity:

a = [] b = [] c = assert(a == b) # good, 2 lists compare equal per list.__eq__ assert(a b) # fails, 2 names don't refer same object assert(a c) # good, c , point same list 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -