[英]What is the difference between 'in' and 'is' when used in an 'if' statement?
I just started Python two days ago and I came across 2 types of if
statements: one where is
was used and another where in
was used. 我刚开始Python的前两天,我在2种类型来了if
语句:一个地方is
使用而另一种in
使用。 So what is basically the difference between them? 那么它们之间的基本区别是什么?
The is keyword is used to test if two variables refer to the same object. is关键字用于测试两个变量是否引用同一对象。 Example: 例:
x = ["apple", "banana", "cherry"]
y = x
print(x is y)
The in keyword is used to check if a value is present in a sequence (list, range, string etc.). in关键字用于检查序列(列表,范围,字符串等)中是否存在值。 Example: 例:
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("yes")
Those are not "part" of an IF statement per-se. 这些本身不是IF语句的“一部分”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.