Python中,元组tuple与list类似,不同之处在于tuple的元素不能修改,tuple使用(),list使用[],

(1)元组的创建使用(),需要注意的是创建包含一个元素的元组:

 tuple_number = ()
tuple_number = (1, ) #创建一个元素的元组,在元素后加逗号
print("type of (1) is:", type((1))) #(1)的类型是整形 type of (1) is: <class 'int'>

(2)元组的索引,切片,检查成员,加,乘

 #索引
tuple_number = (1, 2, 3, 4, 5)
print("tuple_number[2]:", tuple_number[2])
#切片
print("tuple_number[1:4]:", tuple_number[1:4])#index = 4的元素不包含
#检查成员
if 6 in tuple_number:
print("6 is in tuple_number")
else:
print("6 is not in tuple_number")
#加
tuple_name = ('John', 'Paul')
print("tuple_number plus tuple_name:", tuple_number + tuple_name)
#乘
print("tuple_number * 2:", tuple_number * 2)

Code

 tuple_number[2]: 3
tuple_number[1:4]: (2, 3, 4)
6 is not in tuple_number
tuple_number plus tuple_name: (1, 2, 3, 4, 5, 'John', 'Paul')
tuple_number * 2: (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

Result

(3)tuple的遍历和list一样: for number in tuple_number:print(number)

(4)与list一样,tuple也有函数:len(),max(),min(),tuple()

(5)由于tuple的元素不允许修改,tuple的内置方法只有count(),index()

 #方法
tuple_number = (1, 1, 2, 2, 2, 3, 3, 3, 3)
print("count of 2 in tuple_number:", tuple_number.count(2)) #元素出现的次数
print("index of first 3:", tuple_number.index(3)) #元素第一次出现的位置

Code

 count 2 in tuple_number: 3
index of first 3: 5

Result

(6)最后看看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

Class Tuple

基础数据类型-tuple的更多相关文章

  1. Python - 基础数据类型 tuple 元组

    元组简单介绍 元组是一个和列表和相似的数据类型,也是一个有序序列 两者拥有着基本相同的特性,但是也有很多不同的地方 声明元组 var = (1, 2, 3) var = ("1", ...

  2. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  3. python基础数据类型--元组(tuple)

    python基础数据类型--元组(tuple) 一.元组的定义和特性 定义:与列表相似,只不过就是将[ ] 改成 ( ) 特性:1.可以存放多个值 2.不可变 3.按照从左到右的顺序定义元组元素,下标 ...

  4. python的学习笔记01_4基础数据类型列表 元组 字典 集合 其他其他(for,enumerate,range)

    列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问 ...

  5. python学习日记(基础数据类型及其方法02)

    python的变量 python中的变量不需要声明,变量载使用前必须被赋值,变量被赋值以后才会被创建. 在python中变量就是变量,没有数据类型.我们所说的类型是变量所指向内存中的对象的类型. py ...

  6. python学习日记(基础数据类型及其方法01)

    数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...

  7. day 7 - 1 集合、copy及基础数据类型汇总

    集合:{},可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复.(不重要)集合的书写 set1 = set({1,2,3}) #set2 = {1,2,3,[2,3],{'name':' ...

  8. python基础篇_002_基础数据类型

    Python基础数据类型 1.int # int 用于计算 num = 3 # int 与其他数据类型转换 int_to_str = str(num) # 数字加引号 print(int_to_str ...

  9. 二: python基础数据类型(int,

    一.什么是数据类型?2018-12-20   20:57:3õ # (3) num = 0 while num < 10: num += 1 if num == 7: num += 1 # 7执 ...

随机推荐

  1. Git&GitHub-初步使用

    Git 1.安装 下载安装包,安装,默认安装了 Git GUI Here 和 Git Bash Here. 需要在哪里使用 git,只需在文件夹空白处右键,选择Git Bash Here即可打开 gi ...

  2. mongo配置项说明

    mongo configure 配置文件   storage:     dbPath: mongod实例存储其数据的目录.     indexBuildRetry: 指定是否mongod在下次启动时重 ...

  3. Centos6.5 VM网络故障,可以Ping 通网关,无法上网或者访问其它网段

    首先查看cat /etc/sysconfig/network-scripts/ifcfg-eth0  配置是否正确 查看cat /etc/udev/rules.d/70-persistent-net. ...

  4. js 中~~是什么意思?

    其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1~~false == 0~~"" == 0~~[] == 0 ~~undefined ==0~~!undef ...

  5. laydate5.0 设置最大最小值

    由于新版的laydate时间插件在初始化时已设置时间最大最小范围,且生成对象,无法重新渲染改变其日期最大最小值. 有网友经实验贴出如下方法可达成目的,故做记录. //开始时间 var startDat ...

  6. Macbook下安装memcached

    参考文献: https://blog.csdn.net/weixin_41827162/article/details/82049520 感谢大佬 安装memcached需要Homebrew 注意点: ...

  7. Hive(4)-Hive的数据类型

    一. 基本数据类型 Hive数据类型 Java数据类型 长度 例子 TINYINT byte 1byte有符号整数 20 SMALINT short 2byte有符号整数 20 INT int 4by ...

  8. 新系统设置 github 私钥

    1.首先我得重新在git设置一下身份的名字和邮箱(因为当初都忘了设置啥了,因为遇到坑了)进入到需要提交的文件夹底下(因为直接打开git Bash,在没有路径的情况下,根本没!法!改!刚使用git时遇到 ...

  9. 【NXP开发板应用—智能插排】2.初步解析example之GPI

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  10. 使用GlobalKey启动APP

    按键输入有三种:system key  音量键 global key 按下启动某个APP user key  ABCD... 给安卓应用程序定义一个广播接收者,写一个BroadcastReceiver ...