Sep-19-2018, 03:00 AM
... coding tips like this?
if you have ever coded something like this:
if you have ever coded something like this:
if n==1: s = '' else: s = 's' ... print('we found',n,'thing'+s)or the shorter way many people hate:
s = '' if n==1 else 's' ... print('we found',n,'thing'+s)then you might want to consider doing it like this:
s = ('s','')[n==1] ... print('we found',n,'thing'+s)this works because booleans False and True can be used anywhere a number can be used:
Output:>>> complex(False)
0j
>>> complex(True)
(1+0j)
>>> True/2
0.5
>>>
note that the last one will give a different result in Python2.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.