python中tuple函数

编程入门 行业动态 更新时间:2024-10-25 15:28:02

python中tuple<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数"/>

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函数

本文发布于:2024-03-23 23:16:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   python   tuple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!