python的in,is和id函数
1. in 和 not in —— 判断某个序列中是否存在某值
# in aa = [1,2,3,‘Cathy‘,‘太平洋‘] if ‘大西洋‘ in aa: print(‘yes‘) else: print(‘no‘) # no # not in if ‘大西洋‘ not in aa: print(‘yes‘) # yes else: print(‘no‘) #------------------------------------------------------- # 判断字符串是否存在某子串 if ‘马来西亚‘ in ‘马来西亚是一个太平洋国家‘: print(‘of course‘) # 输出 ‘of course‘ # ------------------------------------------------------ if 0 in (0,1,2,3): print(‘hhhh‘) #输出 ‘hhhh‘ if 0 in [0,1,2,3]: print(‘kkkk‘) #输出 ‘hhhh‘
2. is 和 is not —— 判断两个标识符是不是指向同一个地址(同一个对象),类似于判断 id(x) == id(y)
# 相同 bb = 234 cc = 234 if bb is cc: print(‘bb is cc‘) #结果是 bb is cc else: print(‘bb is not cc‘) print(id(bb)) # id()值一样 print(id(cc)) # 相同 bb = ‘hhh‘ cc = ‘hhh‘ if bb is cc: print(‘bb is cc‘) #结果是 bb is cc else: print(‘bb is not cc‘) # 相同 bb = ‘hhh‘ cc = bb if bb is cc: print(‘bb is cc‘) #结果是 bb is cc else: print(‘bb is not cc‘) print(id(bb)) # id()值一样 print(id(cc)) # 相同 bb = [1,2,3] cc = bb if bb is cc: print(‘bb is cc‘) #结果是 bb is cc else: print(‘bb is not cc‘) # 不相同 bb = [1,2,3] cc = [1,2,3] if bb is cc: print(‘bb is cc‘) else: print(‘bb is not cc‘) #结果是 bb is not cc print(id(bb)) # id()值不一样 print(id(cc))
3. id() 函数,获取对象的内存地址(不同电脑不一样),例如:
bb = ‘hhh‘ cc = bb if bb is cc: print(‘bb is cc‘) #结果是 bb is cc else: print(‘bb is not cc‘) print(id(bb)) # id()值一样,均为 1680346901336 print(id(cc))
参考:
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16