Python09:元组

时间:2018-04-07 17:11:49   收藏:0   阅读:168

元组跟列表差不多,都是存一组数据,但不同的是,列表可以修改,元组不能,元组定义好之后只能查看,切片,元组又叫只读列表。Python中用()表示元素。它只有两个方法:count()index()

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

names = ("one","two","three","one","two")
print(names.count("one"))
print(names.index("three"))
print(names.index("two"))

 

输出:

2

2

1

 

Process finished with exit code 0

解释:

print(names.count("one")):统计元组中元素的个数。

print(names.index("three")):查询元素中元素的下标。

print(names.index("two")):查询元素中元素的下标,当有多个时,返回所查询的第一个元素的下标。

原文:https://www.cnblogs.com/mclind/p/8733442.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!