Python成員運算子

2022-06-19 13:03:13 字數 2341 閱讀 7080

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 是否在 dic 中

#字典主要是看,是否存在該鍵

print(a in

dic)

#true

#判斷 d 是否在 s 中

print(d in

dic)

#false

a = 'a'

d = 'd'

s =

#判斷 a 是否在 s 中

print(a ins)#

true

#判斷 d 是否在 s 中

print(d ins)#

false

#not in , 不在返回 true ,在返回 false

#分別在列表、字串、元組、字典和集合中使用。

a = 'a'

d = 'd'

lst = ['

a','

b','c'

]#判斷 a 是否不在 lst 中

print(a not

inlst)

#false

#判斷 d 是否在 lst 中

print(d not

inlst)

#true

a = 'a'

d = 'd'

strs = '

abc'

#判斷 a 是否不在 strs 中

print(a not

instrs)

#false

#判斷 d 是否不在 strs 中

print(d not

instrs)

#true

a = 'a'

d = 'd'

tup = ('

a','

b','c'

)#判斷 a 是否不在 tup 中

print(a not

intup)

#false

#判斷 d 是否不在 tup 中

print(d not

intup)

#true

a = 'a'

d = 'd'

dic =

#字典主要是看,是否存在該鍵

#判斷 a 是否不在 dic 中

print(a not

indic)

#false

#判斷 d 是否不在 dic 中

print(d not

indic)

#true

a = 'a'

d = 'd'

s =

#判斷 a 是否不在 s 中

print(a not

ins)

#false

#判斷 d 是否不在 s 中

print(d not

ins)

#true

2020-02-05

Python 成員運算子in not in

in 如果在指定的序列中找到值返回 true,否則返回 false。x 在 y 序列中 如果 x 在 y 序列中返回 true。not in 如果在指定的序列中沒有找到值返回 true,否則返回 false。x 不在 y 序列中 如果 x 不在 y 序列中返回 true。is is 是判斷兩個識別符...

Python成員運算子 in not in

運算子 示例in x in y序列,如果x在y序列中,返回true,否則返回false not in x not in y序列,如果x不在y序列中,返回true,否則返回false a 3b 30list1 1 2,3 4,5 if a in list1 print 變數a 在列表list1中 el...

python 成員運算子 in 和 not in

python成員運算子測試給定值是否為序列中的成員,例如字串,列表或元組。有兩個成員運算子,如下所述 in 如果在指定的序列中找到乙個變數的值,則返回true,否則返回false。not in 如果在指定序列中找不到變數的值,則返回true,否則返回false。coding utf 8 a 10 b...