zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python成员运算符

Python 运算符 成员
2023-09-11 14:21:27 时间

Python成员运算符:

  in:如果左面的对象右面的对象中,则返回 True,不在则返回 False。

 not in:如果左面的对象不在右面的对象中,则返回 True,在则返回 False。

# 分别在列表、字符串、元组、字典和集合中使用。
# in 在则返回 True , 不在则返回 False
a = 'a'
d = 'd'
lst = ['a','b','c']
# 判断 a 是否在 lst 中
print(a in lst)
# True
# 判断 d 是否在 lst 中
print(d in lst)
# False

a = 'a'
d = 'd'
strs = 'abc'
# 判断 a 是否在 strs 中
print(a in strs)
# True
# 判断 d 是否在 strs 中
print(d in strs)
# False

a = 'a'
d = 'd'
tup = ('a','b','c')
# 判断 a 是否在 tup 中
print(a in tup)
# True
# 判断 d 是否在 tup 中
print(d in tup)
# False

a = 'a'
d = 'd'
dic = {'a':123,'b':456,'c':789}
# 判断 a 是否在 dic 中
# 字典主要是看,是否存在该键
print(a in dic)
# True
# 判断 d 是否在 s 中
print(d in dic)
# False

a = 'a'
d = 'd'
s = {'a','b','c'}
# 判断 a 是否在 s 中
print(a in s)
# True
# 判断 d 是否在 s 中
print(d in s)
# False

# not in , 不在返回 True ,在返回 False
# 分别在列表、字符串、元组、字典和集合中使用。
a = 'a'
d = 'd'
lst = ['a','b','c']

# 判断 a 是否不在 lst 中
print(a not in lst)
# False
# 判断 d 是否在 lst 中
print(d not in lst)
# True

a = 'a'
d = 'd'
strs = 'abc'

# 判断 a 是否不在 strs 中
print(a not in strs)
# False
# 判断 d 是否不在 strs 中
print(d not in strs)
# True

a = 'a'
d = 'd'
tup = ('a','b','c')

# 判断 a 是否不在 tup 中
print(a not in tup)
# False
# 判断 d 是否不在 tup 中
print(d not in tup)
# True

a = 'a'
d = 'd'
dic = {'a':123,'b':456,'c':789}
# 字典主要是看,是否存在该键

# 判断 a 是否不在 dic 中
print(a not in dic)
# False
# 判断 d 是否不在 dic 中
print(d not in dic)
# True

a = 'a'
d = 'd'
s = {'a','b','c'}
# 判断 a 是否不在 s 中
print(a not in s)
# False
# 判断 d 是否不在 s 中
print(d not in s)
# True

2020-02-05