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 读写、创建 文件的方法(必看)
Sep 12 Python
详解PyTorch批训练及优化器比较
Apr 28 Python
Python实现的读取电脑硬件信息功能示例
May 30 Python
python opencv实现切变换 不裁减图片
Jul 26 Python
Python中的random.uniform()函数教程与实例解析
Mar 02 Python
Python爬虫——爬取豆瓣电影Top250代码实例
Apr 17 Python
通过PYTHON来实现图像分割详解
Jun 26 Python
matplotlib quiver箭图绘制案例
Apr 17 Python
python 中的命名空间,你真的了解吗?
Aug 19 Python
装上这 14 个插件后,PyCharm 真的是无敌的存在
Jan 11 Python
python用字节处理文件实例讲解
Apr 13 Python
详细介绍python操作RabbitMq
Apr 12 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 组件化编程技巧
2009/06/06 PHP
第4章 数据处理-php字符串的处理-郑阿奇(续)
2011/07/04 PHP
PHP正则表达式之定界符和原子介绍
2012/10/05 PHP
PHP实现补齐关闭的HTML标签
2016/03/22 PHP
Yii2创建多界面主题(Theme)的方法
2016/10/08 PHP
php分页查询的简单实现代码
2017/03/14 PHP
PHP基于PDO实现的SQLite操作类【包含增删改查及事务等操作】
2017/06/21 PHP
Three.js源码阅读笔记(基础的核心Core对象)
2012/12/27 Javascript
JQuery结合CSS操作打印样式的方法
2013/12/24 Javascript
javascript中实现兼容JAVA的hashCode算法代码分享
2020/08/11 Javascript
JS往数组中添加项性能分析
2015/02/25 Javascript
javascript数组排序汇总
2015/07/07 Javascript
js过滤HTML标签完整实例
2015/11/26 Javascript
文件上传,iframe跨域数据提交的实现
2016/11/18 Javascript
JavaScript实现数组降维详解
2017/01/05 Javascript
JS基于开关思想实现的数组去重功能【案例】
2019/02/18 Javascript
详解babel升级到7.X采坑总结
2019/05/12 Javascript
Vue CLI3基础学习之pages构建多页应用
2019/06/02 Javascript
在博客园博文中添加自定义右键菜单的方法详解
2020/02/05 Javascript
vue样式穿透 ::v-deep的具体使用
2020/06/04 Javascript
原生js实现放大镜组件
2021/01/22 Javascript
[03:51]吞吞映像 每周精彩击杀top10第二弹
2014/06/25 DOTA
Python的几个高级语法概念浅析(lambda表达式闭包装饰器)
2016/05/28 Python
Python实现京东秒杀功能代码
2019/05/16 Python
django mysql数据库及图片上传接口详解
2019/07/18 Python
Python Django 添加首页尾页上一页下一页代码实例
2019/08/21 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
2019/11/08 Python
Python3 ID3决策树判断申请贷款是否成功的实现代码
2020/05/21 Python
职专应届生求职信
2013/11/16 职场文书
超市端午节活动方案
2014/01/23 职场文书
养牛场项目建议书
2014/05/13 职场文书
我的中国梦演讲稿500字
2014/08/19 职场文书
加强作风建设工作总结
2014/10/23 职场文书
党员群众路线整改措施及今后努力方向
2014/10/28 职场文书
致青春观后感
2015/06/09 职场文书
2020优秀员工演讲稿(三篇)
2019/10/17 职场文书