1.元组:tup
1.1当元组中只有一个元素时,需要加“ , ”? ? 否则为字符串类型?
tup1 = ("Tom")
print(type(tup1))
tup2 = ("Tom",)
print(type(tup2))
1.2元组中的索引和切片操作[起始位,结束位,步数]
tup = ("Tom", 20, 180, 60)
print(tup[0])
print(tup[-3])
print(tup[::-2]) # 负数表示倒着输出,2表示隔一个输出
1.3获取长度函数:len( )?
tup = ("Tom", 20, 180, 60)
s = "123456wsl"
print(len(tup))
print(len(s))
1.4元组中添加元素:tup1+tup2
tup = ("Tom", 20, 180, 60)
tup2 = ("man",)
print(tup+tup2)
tup3 = tup+tup2
print(tup3)
2.字典:dict
2.1通过键(key)获取值(value)
D = {"name": "Alex", "Age": "18", "gander": "man"}
print(D["name"])
# dict.get(key,默认值)通过key获取值,如果不存在,返回默认值
print(D.get(name))
2.2添加键值对
D = {"name": "Alex", "Age": "18", "gander": "man"}
# 添加键值对
D["height"] = 180
print(D)
# 添加多个键值对
D.update([[1, "A"], ["weight", "60"]])
print(D)
2.3修改键值对
D1 = {"name": "Alex", "Age": "18", "gander": "man"}
D1["name"] = "Tony"
print(D1)
2.4清空全部键值对
D1 = {"name": "Alex", "Age": "18", "gander": "man"}
D1.clear()
print(D1)
2.5拿出字典中的全部键、值、键值对
D = {"name": "Alex", "Age": "18", "gander": "man"}
print(D.keys())
print(D.values())
print(D.items())
2.6快速生成一个字典: {}.formkeys()?
# {}.formkeys()方法生成的字典为一对多!!!
D2 = ['name','age','height']
D3 = ["Alex", "18", "182"]
print({}.fromkeys(D2, D3))
|