Python - 元组(tuple) 详解 及 代码
元组(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) 详解 及 代码的更多相关文章
- Python - 字典(dict) 详解 及 代码
字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...
- Python推导式详解,带你写出比较精简酷炫的代码
Python推导式详解,带你写出比较精简酷炫的代码 前言 1.推导式分类与用法 1.1 列表推导 1.2 集合推导 1.3 字典推导 1.4 元组推导?不存在的 2.推导式的性能 2.1 列表推导式与 ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- python之struct详解
python之struct详解 2018-05-23 18:20:29 醉小义 阅读数 20115更多 分类专栏: python 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议 ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- Python中dict详解
from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...
- python/ORM操作详解
一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...
- 深度学习之卷积神经网络(CNN)详解与代码实现(一)
卷积神经网络(CNN)详解与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10430073.html 目 ...
- python之数据类型详解
python之数据类型详解 二.列表list (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...
随机推荐
- php 微信3 自定义菜单
<pre name="code" class="php"><pre name="code" class="htm ...
- Apache提示You don't have permission to access / on this server问题解决
测试时遇到将一本地目录设置为一apache的虚拟主机,在httpd-vhosts.conf文件中进行简单设置,然后在hosts文件中将访问地址指向本地,启动apache,进行访问,却出现了You do ...
- Php会员权限
<?phpecho $uu=@array_sum(@$_POST['gr']);?><form action="" method="POST" ...
- Application_Start
这个时间是在第一次访问网站的时候触发..比如你发布了一个网站,,我第一个来访问,,就会触发这个事件..以后再有人来访问就不会触发了.. 一般的,我们总是以为Application_Start是一个应用 ...
- WPF Window对象
户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互.窗口的主要用途是承载可视化数据并使用户可以与数据进行交互的内容.独立 WPF 应用程序使 ...
- Java实现Http服务器(三)
下面重点介绍上篇文章介绍的HttpServerImpl类当中的ServerImpl类 sun.net.httpserver.ServerImpl 600行左右的类,是整个HttpServer的核心 ...
- jQuery.Deferred对象
一.前言 jQuery1.5之前,如果需要多次Ajax操作,我们一般会使用下面的两种方式: 1).串行调用Ajax $.ajax({ success: function() { $.ajax({ su ...
- 《Braid》碎片式台词
谁见到过风? 你没有,我也没有. 但当树儿低下头, 便是风儿经过时. 便是风儿穿过的时候. 但当树叶微微摇首, 你没有,我也没有. 谁见到过风? 二.时间与宽恕 1.提姆要出发了!他要去寻找并救出公主 ...
- jquery事件之event.target用法详解
1. 定义和用法: 显示哪个 DOM 元素触发了事件: $("p, button, h1, h2").click(function(event){ $("div" ...
- iOS内存管理系列之二:自动释放与便捷方法
有时候一个所有者创建一个对象后,会立刻将该对象的指针传递给其它所有者.这时,这个创建者不希望再拥有这个对象,但如果立刻给它发送一个release消息会导致这个对象被立刻释放掉——这样其它所有者还没有来 ...