python-元组方法(tulpe)知识整理
程序员文章站
2022-04-19 20:33:38
#!/usr/bin/env python# -*- coding:utf-8 -*-# __author__:anxu.qi# Date:2018/11/21################################## 元组 ################################ ......
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__:anxu.qi
# date:2018/11/21
################################## 元组 ###############################
# 元组和列表几乎是一样的
# 列表是可以进行修改的,元组是不能修改的
name_list = ("beijing","shanghai","jinan","shandong")
## 索引
print(name_list[0])
# beijing
## len
print(len(name_list))
# 4
## 切片
print(name_list[0:2])
# ('beijing', 'shanghai')
## for 循环
for i in name_list:
print(i)
"""
beijing
shanghai
jinan
shandong
"""
################################## 删除元组元素报错 ###############################
name_list = ("beijing","shanghai","jinan","shandong")
# del name_list[0]
# typeerror: 'tuple' object doesn't support item deletion (说的是“tuple”对象不支持删除项)
################################## count 统计个数 ###############################
# def count(self, value):
# 统计元素个数
name_list = ("beijing","shanghai","jinan","shandong","beijing")
print(name_list.count("beijing"))
# 2
################################## index 获取索引 ###############################
# def index(self, value, start=none, stop=none):
# 获取指定元素的下标位置
name_list = ("beijing","shandong","shanghai","jinan","shandong")
print(name_list.index('shandong')) # 获取的下标只能从左往右
# 1
################################## 元组 ###############################