简体   繁体   中英

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. So what is basically the difference between them?

The is keyword is used to test if two variables refer to the same object. 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.). Example:

fruits = ["apple", "banana", "cherry"]

if "banana" in fruits:
  print("yes")

Those are not "part" of an IF statement per-se.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM