python 示例
Python是关键字 (Python is keyword)
is is a keyword (case-sensitive) in python, it is used to check whether two objects are the same objects or not.
is是python中的关键字(区分大小写),用于检查两个对象是否相同。
Note: If two variables refer to the same objects then they are the same objects else objects are not the same objects even they contain the same value.
注意:如果两个变量引用相同的对象,则它们是相同的对象,否则即使它们包含相同的值,对象也不是相同的对象。
Syntax of is keyword
is关键字的语法
if (object1 is object2):
statement(s)
Example:
例:
Input:
# create a list
list1 = [10, 20, 30, 40, 50]
# creating list2 with list1
list2 = list1
# condition
if (list1 is list2):
print("Yes")
else:
print("No")
Output:
Yes
is关键字的Python示例 (Python examples of is keyword)
Example 1: Use of is keyword with different objects having same values.
示例1:将is关键字与具有相同值的不同对象一起使用。
# python code to demonstrate example of
# is keyword
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]
# checking
if (list1 is list2):
print("list1 and list2 are the same objects")
else:
print("list1 and list2 are not the same objects")
Output
输出量
list1 and list2 are not the same objects
Example 2: Use of is keyword with the same objects.
示例2:将is关键字与相同的对象一起使用。
# python code to demonstrate example of
# is keyword
# create a list
list1 = [10, 20, 30, 40, 50]
# creating list2 with list1
list2 = list1
# checking
if (list1 is list2):
print("list1 and list2 are the same objects")
else:
print("list1 and list2 are not the same objects")
Output
输出量
list1 and list2 are the same objects
翻译自: https://www.includehelp.com/python/is-keyword-with-example.aspx
python 示例