Python基础数据类型-Tuple(元组)
2022/7/21 14:22:58
本文主要是介绍Python基础数据类型-Tuple(元组),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
a = () b = (1) # 不是元组类型,是int型 c = (1,) # 只有一个元素的时候,要加逗号才能表示是元组 d = (1, 2, 3, 4, 5, 6, 1) print(type(a), type(b), type(c)) # <class 'tuple'> <class 'int'> <class 'tuple'> print(d.index(1)) # return first index of value 返回值的第一个索引 print(d.count(1)) # return number of occurrences of value 返回值的出现的次数 print("===以上为tuple的内置方法===") # 除了以上方法外,常用的方法有: print(d[2]) # 访问索引为2对应的值, 返回3 # d[0]=888 #TypeError: 'tuple' object does not support item assignment 类型错误:tuple 对象不支持项分配。 # tuple 类型一旦初始化就不能修改 print(d + c) # del d[4] #TypeError:“元组”对象不支持项目删除 print(d) del c # 删除整个元组 print(len(d)) #计算元组的长度为7 print(max(d)) # 最大值是6 print(min(d)) #最小值的1 print(d[2:]) #返回索引为2到最后一个元素的值
这篇关于Python基础数据类型-Tuple(元组)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python