【发布时间】:2017-04-24 11:52:31
【问题描述】:
我从 1989 年的 K&R 书中学习了去年夏天的 C。我现在正在学习 python3。
我有些困惑。
如果我在 C 中进行测试
如果 !(.....) '!'将 '( )' 中的值更改为相反的值,因此如果为真,则变为假,反之亦然。
所以我想做的就是把它从 C 改成 python。
int card(long long number, int size){
if(!(size == 13 || size == 15 || size == 16)) { // test to see if valid size.
printf("INVALID\n");
return 0;
}
当我尝试时
def card(number, size):
if !(size == 13 or size == 15 or size == 16):
print("INVALID")
return 0
我遇到语法错误。
所以在网上搜索后,我在python3文档中发现“不是”。
所以我在交互式终端上试试。
>>> if is not (size == 13 or size == 15 or size == 16):
File "<stdin>", line 1
if is not (size == 13 or size == 15 or size == 16):
^
解释器说“是”是语法错误。所以我决定删除它,看看会发生什么。它奏效了!
我在网上搜索,没有'is'我找不到'not'?
可以在 C 中使用 'if not ( ......)' 作为 'if !(......)' 的等价物吗?还是我会遇到问题?还是有不同的 python 方式来做到这一点?
【问题讨论】:
-
只要简单地做
if not (...):;在 python 中,is是身份比较运算符(即检查分配给给定变量的值是否与另一个相同)。 -
另外,
is not也是一个运算符。相关:stackoverflow.com/questions/18275616/…
标签: python python-3.x boolean