While I was testing ternary operator in python 3, I came across this weird phenomenon
import string
test = ""
testing = chr(0) if chr(0) in test else ""
for c in string.ascii_letters + testing:
print(c)
would print a~Z 1 character per line, but
import string
test = ""
for c in string.ascii_letters + chr(0) if chr(0) in test else "":
print(c)
would print nothing.
Can somebody give me an explanation?
for c in string.ascii_letters + (chr(0) if chr(0) in test else "")and it will work.testor theifstatement being used? as just doing thisfor c in string.ascii_letters: print(c)gives the same functionality