函数"/>
python中tuple函数
元组(tuple)类型和函数
题目1:tuple1 = ("Python","java",20,20.1)
print(tuple1[2]) # 通过索引取20的值
del(tuple1) # 删除整个元组
函数
题目2:tuple1 = (20,30,40,50)
len(tuple) # 统计元组返回个数
max(tuple) # 返回元组中最大数字
min(tuple) # 返回元组中最小数字
print(list(tuple1)) # 将元组转化为列表
print(tuple(tuple1)) # 将列表转化为元组
字典(dict)类型和函数
题目1:dict1 = {"name:Python","age:20","a":10}
增:
dict1["分数"] = 10 # 给dict1字典增加一组元素
dict2.setdefault("今天","明天") # 通过函数setdefault增加新的元素
删:
del dict2["a"] # 通过del函数删除字典"a"中元素
dict1.pop(age) # 通过pop函数删除字典中name元素并返回值
dict1.clear() # 通过clear函数清空列表
题目2 如:dict2 = {"a":3,"b":4} dict1 = {"name:Python","age:20","a":10}
改:
dict1["name"] = "java" # 把name中的python修改为java则如果没有name则增加
dict1.update(dict2) # 把dict2字典元素更新到dict1里面去。输出结果dict1在前面
dict2.update(dict1) # 反过来就是把dict1字典元素更新到dict2里面去,输出结果dict2在前面
查:
print(len(dict1)) # 统计字典元素
print(str(dict1)) # 字典转化为可打印的字符串
print(type(dict)) # 查看字典类型 print
print(dict1.keys()) # 返回字典中的键也就是key
print(dict1.values()) # 返回字典中的值也是values
更多推荐
python中tuple函数
发布评论