简体   繁体   English

在“ if”语句中使用“ in”和“ is”有什么区别?

[英]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.

相关问题 '/' 和 '//' 用于除法时有什么区别? - What is the difference between '/' and '//' when used for division? 这两个简单的代码有什么区别? 更具体地说,这个 if 语句是做什么用的? - What's the difference between these two simple codes? More specifically, what is this if statement used for? Python 中的表达式和语句有什么区别? - What is the difference between an expression and a statement in Python? Python 中的语句和函数有什么区别? - What is the difference between a statement and a function in Python? Python中的__set__和__setattr__有什么区别?何时使用? - What is the difference between __set__ and __setattr__ in Python and when should which be used? 在Django过滤器语句中__in和等号(=)之间有什么区别? - In Django filter statement what's the difference between __in and equal sign (=)? if-else语句和pytorch中的torch.where有什么区别? - what is the difference between if-else statement and torch.where in pytorch? Python和Java中import语句的含义有什么区别? - What's the difference between the meaning of import statement in Python and Java? 生成器中的raise StopIteration和return语句有什么区别? - What is the difference between raise StopIteration and a return statement in generators? 关键字或语句与函数调用之间有什么区别? - What's the difference between a keyword or a statement, and a function call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM