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到最后一个元素的值

 

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...