How to write inline if statement for print?
问题 I need to print some stuff only when a boolean variable is set to True . So, after looking at this, I tried with a simple example: >>> a = 100 >>> b = True >>> print a if b File "<stdin>", line 1 print a if b ^ SyntaxError: invalid syntax Same thing if I write print a if b==True . What am I missing here? 回答1: Python does not have a trailing if statement . There are two kinds of if in Python: if statement: if condition: statement if condition: block if expression (introduced in Python 2.5)