数据类型公共方法
数据类型公共方法
“+” 可以用来拼接字符串、元组、列表
>>> words1 = ["a", 'b', 'c']
>>> wwords2 = ["d", "e", "f"]
>>> words1 + wwords2
['a', 'b', 'c', 'd', 'e', 'f']
>>> words1 = ("a", 'b', 'c')
>>> words2 = ("d", "e", "f")
>>> words1 + words2
('a', 'b', 'c', 'd', 'e', 'f')
>>> a1 = 'words'
>>> b1 = 'hello'
>>> a1 + b1
'wordshello'
“-” 只能在集合
>>> A = {'a','b','c'}
>>> B = {'c','d','e'}
# B的补集
>>> A - B
{'b', 'a'}
“*” 可以用于元组、字符串、列表,不能用于字典和集合。
# 字符串
>>> 'a' * 3
'aaa'
# 元组
>>> tt = ("a","b","c")
>>> tt * 3
('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
# 列表
>>> tt = ["a","b","c"]
>>> tt * 3
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
in 成员运算符,用来判断是否存在——列表、字符串、元组、字典、集合。
>>> tt
['a', 'b', 'c']
>>> "a" in tt
True
>>> tt = ("a","b","c")
>>> "a" in tt
True
>>> tt = {"a","b","c"}
>>> "a" in tt
True
# 字典判断的是key
>>> persion = {"name": "aaa", "age": 23, "sex":"nam"}
>>> "name" in persion
True
带下标的遍历
words = ["a", 'b', 'c', 'd', 'e']
# 可以返回一个带下标的数据。
# 如果传入的数据是没有顺序的,返回的值也没有顺序。
en = enumerate(words)
for i in en:
print(i)
"""
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
"""