Fun with Python 3's print function
Fun with the print
function
Most Pythonistas learn early on that the print
function can evaluate statements:
print(2>3) >>> False print(7+5==12) >>> True
However, I didn't know that print
can return different strings based upon whether or not the following statement is true or false. The format is:
print(['output for False', 'output for True']['statement_to_evaluate'])
For example:
print(['no', 'yes'][2 > 3]) >>> no print(['Go back to math class', 'You know math!'][7 + 5 == 12]) >>> You know math!