元组与列表一样,都是序列。但元组不能修改内容(列表允许)

默认的,元组通过圆括号括起来

1. 使用type函数查看类型

numbers = (1,2,3,4,5,6,7,8,9,0)
print(type(numbers))

2. tuple 函数

tuple 函数的功能与list函数基本一样:以一个序列作为参数并把它转换为元组。

AAA = [1,2,3,4,5]
print (type(AAA))
BBB = tuple(AAA)
print (type(BBB))

元组的基本操作

同其他序列(如:索引,分片,相加,相乘)

1.索引
CCC = (11,22,33,44,55,66,77,88)
print (CCC[0])
print (CCC[4])
print (CCC[-1])

注:使用负数索引,python会从右边开始计算。最后一个元素的位置编号是-1

2.分片
DDDD = (12,222,112,333,44,1234,11111,1,33,455,66667,87787)
print (DDDD[3:6]) # 获取第4个到第6个元素
print (DDDD[-3:]) # 获取最好三个元素
print (DDDD[:3]) # 获取前三个元素 print (DDDD[0:10:2]) # 步长为2分片
print (DDDD[::4]) # 步长为4分片
print (DDDD[::-1]) # 从右到左提取元素
3.序列相加
EE = (2,3,4,5,6)
FF = (11,33,32,88,90)
print (EE+FF)
4.序列相乘
GG = ("I","LOVE","PYTHON","~")
print (GG*5)
HH = (12,23,34,56,78)
print (HH*5)

注:元组的内容不能修改

5.统计元素出现次数count函数
HH = (12,22,33,33,44,44,44,55,12)
print (HH.count(12))
print (HH.count(44))

元组tuple官方文档解析

class tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass

python--元组tuple的更多相关文章

  1. Python 元组 tuple() 方法

    描述 Python 元组 tuple() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为元组. 语法 tuple() 方法语法: tuple(iterable) 参数 iterable -- ...

  2. Python元组tuple(不可变)

    Python元组Tuple(不可变): 元组的特点: 1.元组的初始化: tuple = (1, )  #元组只有一个元素的话,初始化时要加,否则当做元素的普通变量类型处理 tuple = (1, 2 ...

  3. python 元组tuple - python基础入门(14)

    在上一篇文章中我们讲解了关于python列表List的相关内容,今天给大家解释一下列表List的兄弟 – 元组,俗称: tuple. 元组tuple和列表List类似,元组有如下特点: 1.由一个或者 ...

  4. python 元组tuple介绍,使用。

    原文 https://blog.csdn.net/ruanxingzi123/article/details/83184909 一  是什么? # python 元组tuple? ''' 元祖tupl ...

  5. Python—元组tuple

    列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...

  6. Python - 元组(tuple) 详解 及 代码

    元组(tuple) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967 元组是存放任意元素集合,不能修 ...

  7. Python元组(tuple)

    元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...

  8. Python 元组(Tuple)操作详解

    Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号, 列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可 一.创建元组 代码如下: tup1 = (' ...

  9. Python 元组Tuple概念和操作

    # 元组概念:有序的不可变的元素集合 # 和列表的区别就是, 元组元素不能修改 # 定义 # 一个元素的写法 # (666,) t = (666,) #正确写法 t = (666) #错误写法,括号当 ...

  10. Python 元组 (tuple)

    作者博文地址:https://www.cnblogs.com/liu-shuai/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...

随机推荐

  1. 20180322 对DataTable里面的数据进行去重

    对DataTable里面的数据进行去重 DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt. ...

  2. shell脚本循环和信号

    条件判断 if     条件1:then COMMAND elif  条件2:then COMMAND else COMMAND(:)        :  表示pass  不执行任何命令 fi 读取用 ...

  3. OCP-第二节课.md

    第一. MQ(压队列)    PGA    share pool 三层结构 应用服务器--->中间件--->数据库 第二. TCP/IP 第三. 应用层:应用层.表示层.会话层 数据流层: ...

  4. .NET Core 指南 官方

    https://docs.microsoft.com/zh-cn/dotnet/core/index

  5. zha男/女的三种境界

    本文为chedan贴,谈一谈找对象时渣男/女的三种表现,分别对应三种境界,涉世未深的男生女生可加以小心,自身属于zha类型的可略过本文.    另,本文的恋爱观基于两个原则.一是对象应是从朋友到恋人的 ...

  6. 2019.03.23 Http

    自己也要分清楚  看清楚 request,response 一个是请求  一个是相应 行 头    之间还有个空行    体 HttpRequest请求对象(只读) 当用户访问一个视图函数时,Djan ...

  7. [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

    Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...

  8. 备份软件 FreeFileSync 怎么用

    现在,学会使用备份软件对电脑中的重要资料定期进行备份,已经成为许多办公一族的“必修课”.其中,FreeFileSync 作为一款由国外开源社区开发的免费备份软件,由于其支持跨平台(Windows.Li ...

  9. Amber TUTORIAL B1: Simulating a DNA polyA-polyT Decamer

    Section 1: Introduction The input files required (using their default file names): prmtop - a file c ...

  10. gitlab4.0备份还原

    一,备份 备份默认路径查看: gitlab/config/gitlab.yml 中的backup: 默认tmp/backups ====>这个是gitlab/tmp/backups/  可不是系 ...