元组(tuple) 详解 及 代码

本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967

元组是存放任意元素集合,不能修改其内容;

len()是求元组的长度, 可以使用下标标示符("[]")去访问元组的数据;

元组内还可以包含元组, 可以通过新建一个元组, 扩充已有的元素;

代码如下:

# -*- coding: utf-8 -*-

#====================
#File: abop.py
#Author: Wendy
#Date: 2013-12-03
#==================== #eclipse pydev, python3.3 #元组, 表示不能修改的一组的值 zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo)) new_zoo = ('monkey', 'camel', zoo) #新建元组, 不是修改
print('Number of cages in the new zoo are', len(new_zoo))
print('All animals in new zoo are', new_zoo)
print('Animals brought from old zoo are', new_zoo[2]) #把zoo放入new_zoo的第2个位置(从0开始)
print('Last animal brought from old zoo is', new_zoo[2][2]) #元组包含元组
print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2])) #减去当前元素[2]和[2]的长度(3-1+3)

输出:

Number of animals in the zoo is 3
Number of cages in the new zoo are 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animals in the new zoo is 5

Python - 元组(tuple) 详解 及 代码的更多相关文章

  1. Python - 字典(dict) 详解 及 代码

    字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...

  2. Python推导式详解,带你写出比较精简酷炫的代码

    Python推导式详解,带你写出比较精简酷炫的代码 前言 1.推导式分类与用法 1.1 列表推导 1.2 集合推导 1.3 字典推导 1.4 元组推导?不存在的 2.推导式的性能 2.1 列表推导式与 ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. python之struct详解

    python之struct详解 2018-05-23 18:20:29 醉小义 阅读数 20115更多 分类专栏: python   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议 ...

  5. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  6. Python中dict详解

    from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...

  7. python/ORM操作详解

    一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...

  8. 深度学习之卷积神经网络(CNN)详解与代码实现(一)

    卷积神经网络(CNN)详解与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10430073.html 目 ...

  9. python之数据类型详解

    python之数据类型详解 二.列表list  (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...

随机推荐

  1. AdaBoost原理,算法实现

    前言: 当做重要决定时,大家可能综合考虑多个专家而不是一个人的意见.机器学习处理问题也是如此,这就是元算法背后的思路.元算法是对其他算法进行组合的一种方式,前几天看了一个称作adaboost方法的介绍 ...

  2. css3学习--css3动画详解一(animation属性)

    ***介绍的属性并不完全,写的都是我认为容易混淆的难点属性,所以属性会在最后综合案例展示~ 一.Keyframes介绍: Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主 ...

  3. chkconfig-增加一个服务设置服务自启动

    参考 http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html 如何增加一个服务: 1.服务脚本必须存放在/etc/ini ...

  4. [Linux]Vim的安装及使用

    1.安装:$sudo apt-get install vim 2.查看Vim所在路径$whereis vim 3.启动Vim $'/usr/bin/vim.tiny'  4. 退出Vim窗口:Ctrl ...

  5. Sharepoint 问题集锦 - 配置

    错误 在sharepoint designer中编辑List的表单的时候,提示soap:Server服务器无法处理请求. ---> 值不在预期的范围内. 解释: 这个是由于我在本地测试的时候,使 ...

  6. Java实现Http服务器(四)

    (1)HTTPServer的监听启动 sun.net.httpserver.ServerImpl类中启动了Socket监听,ServerImpl的内部类Dispatch类启动了Http服务器的监听 / ...

  7. How to enable/disable EWF

    date: 2015/2/18 Enhanced Write Filter (or EWF) is a component of Windows XP Embedded and Windows Emb ...

  8. ld: symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o for architecture i386

    就是选择的运行版本太低了,点击项目,project,把iOS DeployMent Target改为比较高的版本就行

  9. 严重推荐一个免费开源数据库建模工具软件 --OpenSystemArchitect 4.0

    嘿嘿,对于我这样的新手,这个工具还是很令人兴奋的. 真的是术业有专攻啊.关键还是免费开源 EXCEL,VISO,PPT,PS,CD,FREEHAND不是不可以,只是.人家还是专业点,方便点.. Ope ...

  10. [jobdu]调整数组顺序使奇数位于偶数前面

    这道题的代码没啥好说的,用了O(n)的空间就是水题了.但可以讲一下思考过程.一开始是想O(1)的空间的,然后想从左往右双指针扫,然后根据出现顺序交换遇到的偶数和奇数.但遇到一个问题:1, 2, 3, ...