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的高级Git库 Gittle
Sep 22 Python
Python简单删除目录下文件以及文件夹的方法
May 27 Python
python实现爬取千万淘宝商品的方法
Jun 30 Python
通过python顺序修改文件名字的方法
Jul 11 Python
PYQT5实现控制台显示功能的方法
Jun 25 Python
如何运行.ipynb文件的图文讲解
Jun 27 Python
Python socket模块方法实现详解
Nov 05 Python
Python调用钉钉自定义机器人的实现
Jan 03 Python
pytorch GAN生成对抗网络实例
Jan 10 Python
TensorFlow2.0矩阵与向量的加减乘实例
Feb 07 Python
python opencv 检测移动物体并截图保存实例
Mar 10 Python
使用Django的JsonResponse返回数据的实现
Jan 15 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 SPL标准库之接口(Interface)详解
2015/05/11 PHP
PHP静态成员变量和非静态成员变量详解
2017/02/14 PHP
yii2局部关闭(开启)csrf的验证的实例代码
2017/07/10 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
Prototype使用指南之hash.js
2007/01/10 Javascript
dwr spring的集成实现代码
2009/03/22 Javascript
jQuery中add实现同时选择两个id对象
2010/10/22 Javascript
Jquery ajax执行顺序 返回自定义错误信息(实例讲解)
2013/11/06 Javascript
原生js编写设为首页兼容ie、火狐和谷歌
2014/06/05 Javascript
jQuery实现列表内容的动态载入特效
2015/08/08 Javascript
Bootstrap每天必学之表单
2015/11/23 Javascript
jQuery实现鼠标选中文字后弹出提示窗口效果【附demo源码】
2016/09/05 Javascript
微信小程序 rpx 尺寸单位详细介绍
2016/10/13 Javascript
Vue 过渡(动画)transition组件案例详解
2017/01/22 Javascript
Webpack实现按需打包Lodash的几种方法详解
2017/05/08 Javascript
vue proxyTable 接口跨域请求调试的示例
2017/09/12 Javascript
H5+C3+JS实现五子棋游戏(AI篇)
2020/05/28 Javascript
javascript触发模拟鼠标点击事件
2019/06/26 Javascript
深入理解redux之compose的具体应用
2020/01/12 Javascript
JS图片懒加载技术实现过程解析
2020/07/27 Javascript
浅谈MySQL中的触发器
2015/05/05 Python
python多任务及返回值的处理方法
2019/01/22 Python
Python3转换html到pdf的不同解决方案
2019/03/11 Python
解决Python3下map函数的显示问题
2019/12/04 Python
Python安装依赖(包)模块方法详解
2020/02/14 Python
python 伯努利分布详解
2020/02/25 Python
Python使用for生成列表实现过程解析
2020/09/22 Python
利用canvas实现图片下载功能来实现浏览器兼容问题
2019/05/31 HTML / CSS
澳大利亚领先的女帽及配饰公司:Morgan&Taylor
2019/12/01 全球购物
护理专业个人求职简历的自我评价
2013/10/13 职场文书
12月小学生校园广播稿
2014/02/04 职场文书
写求职信有什么意义
2014/02/17 职场文书
用人单位终止解除劳动合同证明书
2014/10/06 职场文书
2014年团支书工作总结
2014/11/14 职场文书
民政局未婚证明
2015/06/15 职场文书
毕业感言怎么写
2015/07/31 职场文书