Python列表和元组的定义与使用操作示例


Posted in Python onJuly 26, 2017

本文实例讲述了Python列表和元组的定义与使用操作。分享给大家供大家参考,具体如下:

#coding=utf8
print '''''
可以将列表和元组当成普通的“数组”,它能保存任意数量任意类型的Python对象。
列表和元组通过数字索引来访问元素(从0开始)。
列表和元组的区别:
------------------------------------------------------------------------------------
          元组              \             列表
------------------------------------------------------------------------------------
列表元素用中括号[]            \  元组元素用小括号()
元素的个数及元素的值可以改变   \  元素的个数及元素的值不可改变
------------------------------------------------------------------------------------
元组可以看出只读的列表。
列表和元组可以通过使用索引运算符([])和切片运算符([:])可以得到子集
'''
NumberList=[1,2,3,4,5,6,7,8.9,0101,017,0xab]
StringList=['hello',"hello world",'''''goddness''']
MixList=[12,13.2,01,'abc','hello']
NumberTouple=(1,2,3,4,5,6,7,8.9,0101,017,0xab)
StringTouple=('hello',"hello world",'''''goddness''')
MixTouple=(12,13.2,01,'abc','hello')
print "output the element of the NumberList by index--------->",NumberList[0],NumberList[1],NumberList[2],NumberList[-1]
print "output the element of the StringList by index--------->",StringList[0],StringList[1],StringList[2],StringList[-1]
print "output the element of the MixList by index--------->",MixList[0],MixList[1],MixList[2],MixList[-1]
print "output the element of the NumberTouple by index--------->",NumberTouple[0],NumberTouple[1],NumberTouple[2],NumberTouple[-1]
print "output the element of the StringTouple by index--------->",StringTouple[0],StringTouple[1],StringTouple[2],StringTouple[-1]
print "output the element of the MixTouple by index--------->",MixTouple[0],MixTouple[1],MixTouple[2],MixTouple[-1]
print "output the element of the NumberList by slice--------->",NumberList[0:2],NumberList[1:3],NumberList[0:],NumberList[:-1]
print "output the element of the StringList by slice--------->",StringList[0:1],StringList[2:3],StringList[0:],StringList[:-1]
print "output the element of the MixList by slice--------->",MixList[0:],MixList[:1],MixList[0:2],MixList[2:-1]
print "output the element of the NumberTouple by slice--------->",NumberTouple[0:2],NumberTouple[1:3],NumberTouple[2:],NumberTouple[:-1]
print "output the element of the StringTouple by slice--------->",StringTouple[0:2],StringTouple[1:3],StringTouple[2],StringTouple[-1]
print "output the element of the MixTouple by slice--------->",MixTouple[0:],MixTouple[1:3],MixTouple[2],MixTouple[:-1]
NumberList[0]=59
#NumberTouple[0]=56
print "Change the value of NumberList[0] to 59------------",NumberList[0]
#print "Can not change the value of NumberTouple[0] to 56------------",NumberTouple[0]

运行结果:

Python列表和元组的定义与使用操作示例

更多Python相关内容感兴趣的读者可查看本站专题:《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python遍历目录的4种方法实例介绍
Apr 13 Python
Python 查找字符在字符串中的位置实例
May 02 Python
python 实现对文件夹中的图像连续重命名方法
Oct 25 Python
numpy 对矩阵中Nan的处理:采用平均值的方法
Oct 30 Python
Python数据类型之Number数字操作实例详解
May 08 Python
Django在pycharm下修改默认启动端口的方法
Jul 26 Python
使用django和vue进行数据交互的方法步骤
Nov 11 Python
Python3 pickle对象串行化代码实例解析
Mar 23 Python
python开发前景如何
Jun 11 Python
python 6种方法实现单例模式
Dec 15 Python
Python离线安装openpyxl模块的步骤
Mar 30 Python
Python中time标准库的使用教程
Apr 13 Python
老生常谈Python之装饰器、迭代器和生成器
Jul 26 #Python
python基础之入门必看操作
Jul 26 #Python
Python简单定义与使用字典dict的方法示例
Jul 25 #Python
Python学习入门之区块链详解
Jul 25 #Python
Python列表list解析操作示例【整数操作、字符操作、矩阵操作】
Jul 25 #Python
Python中的错误和异常处理简单操作示例【try-except用法】
Jul 25 #Python
Python中函数及默认参数的定义与调用操作实例分析
Jul 25 #Python
You might like
《星际争霸2》终章已出 RTS时代宣告终结
2017/02/07 星际争霸
第九节--绑定
2006/11/16 PHP
把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。
2011/06/24 PHP
php入门学习知识点六 PHP文件的读写操作代码
2011/07/14 PHP
PHP程序漏洞产生的原因分析与防范方法说明
2014/03/06 PHP
php生成圆角图片的方法
2015/04/07 PHP
Gambit vs CL BO3 第一场 2.13
2021/03/10 DOTA
鼠标图片振动代码
2006/07/06 Javascript
Javascript Math对象
2009/08/13 Javascript
juqery 学习之五 文档处理 插入
2011/02/11 Javascript
JS获取网页属性包括宽、高等等
2014/04/03 Javascript
Node.js中的事件驱动编程详解
2014/08/16 Javascript
js实现window.open不被拦截的解决方法汇总
2014/10/30 Javascript
Jquery1.9.1源码分析系列(六)延时对象应用之jQuery.ready
2015/11/24 Javascript
简述Matlab中size()函数的用法
2016/03/20 Javascript
全面解析bootstrap格子布局
2016/05/22 Javascript
js实现获取两个日期之间所有日期的方法
2016/06/17 Javascript
jquery实现网页定位导航
2016/08/23 Javascript
JS针对Array的各种操作汇总
2016/11/29 Javascript
win系统下nodejs环境安装配置
2017/05/04 NodeJs
Bootstrap3.3.7导航栏下拉菜单鼠标滑过展开效果
2017/10/31 Javascript
跟老齐学Python之编写类之四再论继承
2014/10/11 Python
Python连接mssql数据库编码问题解决方法
2015/01/01 Python
Python中super关键字用法实例分析
2015/05/28 Python
python利用OpenCV2实现人脸检测
2020/04/16 Python
Python标准库笔记struct模块的使用
2018/02/22 Python
python构建深度神经网络(续)
2018/03/10 Python
Python hashlib模块加密过程解析
2019/11/05 Python
EQVVS官网:设计师男装和女装
2018/10/24 全球购物
文科教师毕业的自我评价
2014/01/16 职场文书
销售简历自我评价怎么写
2014/09/26 职场文书
党员剖析材料范文
2014/09/30 职场文书
试用期辞职信范文
2015/03/02 职场文书
中国梦宣传标语口号
2015/12/26 职场文书
年终奖金发放管理制度,中小企业适用,拿去救急吧!
2019/07/12 职场文书
导游词之山东红叶谷
2019/10/31 职场文书