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

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

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. wamp支持win10吗?怎么设置?

    上周ytkah总算把系统升级到win10了,可怎么设置wamp支持win10呢?启动wampwerver是处于黄色状态,打开本地页面是空白,应该是端口问题. 单击右下角wamp图标,点Apache,修 ...

  2. 基于Kinetic框架实现超酷的风铃悬挂摆动效果

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/iefreer/article/details/37049987 在踏得网开发过程中,我们在引导页面中 ...

  3. LigerUi自动检索输入

    var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASI ...

  4. python的globals()

    以字典的形式返回当前位置的全局变量

  5. EOS config 大全

    https://eosfans.io/topics/930 bnet_plugin bnet-endpoint: 所监听的传入链接的端点. 默认:0.0.0.0:4321 bnet-follow-ir ...

  6. 万恶之源 - Python模块一

    序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...

  7. 546A

    #include <stdio.h> int main() { int n1,n2,n3; int ans; scanf("%d %d %d", &n1, &a ...

  8. [LeetCode] 110. Balanced Binary Tree_Easy tag: DFS

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. ie8以下兼容圆角等css3的属性

    <!--[if lt IE 10]> <script type="text/javascript" src="PIE.js"></ ...

  10. .Net Core资源

    官网:https://dotnet.github.io/ 1.开发环境 vs2015安装: .net core sdk : https://download.microsoft.com/downloa ...