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内置数据类型详解
Aug 18 Python
python开发中module模块用法实例分析
Nov 12 Python
Python实例一个类背后发生了什么
Feb 09 Python
Python字符串切片操作知识详解
Mar 28 Python
Python文件夹与文件的相关操作(推荐)
Jul 25 Python
利用Python破解斗地主残局详解
Jun 30 Python
python与C互相调用的方法详解
Jul 14 Python
在cmd命令行里进入和退出Python程序的方法
May 12 Python
Python定时任务sched模块用法示例
Jul 16 Python
Python socket实现的简单通信功能示例
Aug 21 Python
python中dict使用方法详解
Jul 17 Python
python 使用xlsxwriter循环向excel中插入数据和图片的操作
Jan 01 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
PHP4中session登录页面的应用
2008/07/25 PHP
swfupload 多文件上传实现代码
2008/08/27 PHP
PHP变量赋值、代入给JavaScript中的变量
2015/06/29 PHP
php使用gd2绘制基本图形示例(直线、圆、正方形)
2017/02/15 PHP
JavaScript高级程序设计 客户端存储学习笔记
2011/09/10 Javascript
深入理解JavaScript系列(6) 强大的原型和原型链
2012/01/15 Javascript
通过伪协议解决父页面与iframe页面通信的问题
2015/04/05 Javascript
js中日期的加减法
2015/05/06 Javascript
Javascript基于对象三大特性(封装性、继承性、多态性)
2016/01/04 Javascript
js表单处理中单选、多选、选择框值的获取及表单的序列化
2016/03/08 Javascript
使用jQuery制作Web页面遮罩层插件的实例教程
2016/05/26 Javascript
JS表格组件BootstrapTable行内编辑解决方案x-editable
2016/09/01 Javascript
微信小程序 框架详解及实例应用
2016/09/26 Javascript
百度搜索框智能提示案例jsonp
2016/11/28 Javascript
jQuery实现获取h1-h6标题元素值的方法
2017/03/06 Javascript
微信小程序 动态传参实例详解
2017/04/27 Javascript
原生JS实现简单的无缝自动轮播效果
2018/09/26 Javascript
JS调用安卓手机摄像头扫描二维码
2018/10/16 Javascript
Element Tooltip 文字提示的使用示例
2020/07/26 Javascript
[03:06]2018年度CS GO最具人气解说-完美盛典
2018/12/16 DOTA
[01:02:30]Mineski vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
python的id()函数介绍
2013/02/10 Python
Python实现自动添加脚本头信息的示例代码
2016/09/02 Python
python使用tcp实现局域网内文件传输
2020/03/20 Python
十分钟搞定pandas(入门教程)
2019/06/21 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
QML使用Python的函数过程解析
2019/09/26 Python
Python内置类型性能分析过程实例
2020/01/29 Python
Python实现计算图像RGB均值方式
2020/06/04 Python
Levi’s美国官网:美国著名的牛仔裤品牌
2016/08/19 全球购物
工程概预算专业毕业生求职信
2013/10/04 职场文书
2014年毕业演讲稿范文
2014/05/13 职场文书
保险公司演讲稿
2014/09/02 职场文书
学习计划书怎么写
2014/09/15 职场文书
2015羊年春节慰问信
2015/02/14 职场文书
作文之亲情600字
2019/09/23 职场文书