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. python函数名应用

    函数名的应用 函数名 的应用分类: 函数就是一个特殊的变量(可以看成一个变量来用) *函数名对应函数的内存地址 *函数名可以做为容器类数据的元素 *函数名可以作为函数的参数 *函数名可以作为函数的返回 ...

  2. jQuery 常用核心方法

    jQuery 常用核心方法 .each() 遍历一个jQuery对象,为每个匹配元素执行一个函数 $('p').each(function(idx,node){ $(node).text(idx + ...

  3. operator.itemgetter() 字典列表排序

    ## 字典列表排序 students = [ {"name": "Stanley", "age": 22, "score" ...

  4. 【tp5.1】通过PHPExcel实现导入excel表格

    1.上github下载PHPExcel,链接:https://github.com/PHPOffice/PHPExcel 2.下载解压后,将Classes改名为PHPExcel如图 3.将文件夹复制到 ...

  5. ffmreg thinkphp 控制器 获取音频视频详细信息(获取时长)

    FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹: 打开你想安装的任意磁盘,例如:d盘.新建一个名为“ffmpeg”的文件夹,将第二步 ...

  6. 学习新框架laravel4 第三天

    请求与输入 获取请求参数 如果没有传递默认值位1 $id= Input::get('id',1); //获取所有请求内容 Input::all() 取得请求 URI $uri = Request::p ...

  7. Java学习笔记十八:Java面向对象的三大特性之封装

    Java面向对象的三大特性之封装 一:面向对象的三大特性: 封装 继承 多态   二:封装的概念: 将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访 ...

  8. 如何制作 Ubuntu 系统的 USB 启动盘

    下载 Ubuntu 打开 http://mirrors.ustc.edu.cn 获取安装镜像 --> 获取 ISO 刻录 Ubuntu 到 U 盘 打开 http://rufus.akeo.ie ...

  9. ov5640介绍

    1 摄像头 在各类信息中,图像含有最丰富的信息,作为机器视觉领域的核心部件,摄像头被广泛地应用在安防.探险以及车牌检测等场合.摄像头按输出信号的类型来看可以分为数字摄像头和模拟摄像头,按照摄像头图像传 ...

  10. Making AJAX Applications Crawlable

    https://developers.google.com/webmasters/ajax-crawling/