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小程序来统计测试脚本的关键字
Mar 12 Python
Python实现批量更换指定目录下文件扩展名的方法
Sep 19 Python
Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
Aug 07 Python
python list是否包含另一个list所有元素的实例
May 04 Python
对python中dict和json的区别详解
Dec 18 Python
python 字典操作提取key,value的方法
Jun 26 Python
python实现抠图给证件照换背景源码
Aug 20 Python
python分布式编程实现过程解析
Nov 08 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
Jan 08 Python
python爬虫开发之urllib模块详细使用方法与实例全解
Mar 09 Python
详解Python 函数参数的拆解
Sep 02 Python
判断Python中的Nonetype类型
May 25 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
利用php来自动调用不同服务器上的flash
2006/10/09 PHP
PHP与MongoDB简介|安全|M+PHP应用实例详解
2013/06/17 PHP
php使用mkdir创建多级目录入门例子
2014/05/10 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(五)
2014/06/23 PHP
一起来写段JS drag拖动代码
2010/12/09 Javascript
在jQuery中 常用的选择器介绍
2013/04/16 Javascript
原生Js实现元素渐隐/渐现(原理为修改元素的css透明度)
2013/06/24 Javascript
IE中getElementsByName()对有些元素无效的解决方案
2014/09/28 Javascript
jQuery插件Slider Revolution实现响应动画滑动图片切换效果
2015/06/05 Javascript
JavaScript中setter和getter方法介绍
2016/07/11 Javascript
vue2滚动条加载更多数据实现代码
2017/01/10 Javascript
利用jQuery异步上传文件的插件用法详解
2017/07/19 jQuery
VSCode配置react开发环境的步骤
2017/12/27 Javascript
js 索引下标之li集合绑定点击事件
2018/01/12 Javascript
通过js动态创建标签,并设置属性方法
2018/02/24 Javascript
Vue formData实现图片上传
2019/08/20 Javascript
Vue 中使用lodash对事件进行防抖和节流操作
2020/07/26 Javascript
[00:30]明星选手化身超级英雄!2018DOTA2亚洲邀请赛全明星赛来临!
2018/04/06 DOTA
[13:25]VP vs VICI (BO3)
2018/06/07 DOTA
有关wxpython pyqt内存占用问题分析
2014/06/09 Python
Python列表append和+的区别浅析
2015/02/02 Python
django之常用命令详解
2016/06/30 Python
python生成圆形图片的方法
2020/03/25 Python
使用Python向C语言的链接库传递数组、结构体、指针类型的数据
2019/01/29 Python
解决keras GAN训练是loss不发生变化,accuracy一直为0.5的问题
2020/07/02 Python
如何利用Python动态模拟太阳系运转
2020/09/04 Python
Python自动化测试基础必备知识点总结
2021/02/07 Python
中国旅游网站:同程旅游
2016/09/11 全球购物
泰国国际航空公司官网:Thai Airways International
2019/12/04 全球购物
伦敦鲜花递送:Flower Station
2021/02/03 全球购物
JAVA代码查错题
2014/10/10 面试题
小学二年级评语
2014/04/21 职场文书
小学六一儿童节活动方案
2014/08/27 职场文书
立志成才演讲稿
2014/09/04 职场文书
中学生爱国演讲稿
2014/09/05 职场文书
社保转移委托书范本
2014/10/08 职场文书