Python:使用Perl样式逻辑非和布尔否定

时间:2014-05-25 19:38:21

标签: python perl operators

这个等效的Perl行的python方法是什么?

! (90 % 15) && print "yes"

1 个答案:

答案 0 :(得分:1)

你可以写

not (90 % 15) and print("yes")

如果您使用python 2(和> = 2.6),则需要显式导入print_function

from __future__ import print_function

这是必需的,因为布尔值and的右操作数必须是expression (and can not be a statement)。在python2中,print是一个语句,因此您需要从print模块中的python3导入__future__函数。请注意,您也可以使用sys.stdout.write("yes\n")来实现相同的目标。

相关问题