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的反射机制
Jun 28 Python
用Python将动态GIF图片倒放播放的方法
Nov 02 Python
Python编写登陆接口的方法
Jul 10 Python
详解python websocket获取实时数据的几种常见链接方式
Jul 01 Python
python getpass模块用法及实例详解
Oct 07 Python
python读取tif图片时保留其16bit的编码格式实例
Jan 13 Python
Python的赋值、深拷贝与浅拷贝的区别详解
Feb 12 Python
Django 用户登陆访问限制实例 @login_required
May 13 Python
django queryset相加和筛选教程
May 18 Python
python闭包与引用以及需要注意的陷阱
Sep 18 Python
查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法
Jan 05 Python
浅谈pytorch中stack和cat的及to_tensor的坑
May 20 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 MsSql server时遇到的中文编码问题
2009/06/11 PHP
php获取json数据所有的节点路径
2015/05/17 PHP
laravel 数据验证规则详解
2019/10/23 PHP
Maps Javascript
2007/01/22 Javascript
jquery ajax 检测用户注册时用户名是否存在
2009/11/03 Javascript
javascript 异步页面查询实现代码(asp.net)
2010/05/26 Javascript
js中巧用cssText属性批量操作样式
2011/03/13 Javascript
13 个JavaScript 性能提升技巧分享
2012/07/26 Javascript
jquery无缝向上滚动实现代码
2013/03/29 Javascript
Document.location.href和.replace的区别示例介绍
2014/03/04 Javascript
jquery获取tagName再进行判断
2014/05/29 Javascript
谈谈JavaScript中function多重理解
2015/08/28 Javascript
学习Angularjs分页指令
2016/07/01 Javascript
js点击按钮实现水波纹效果代码(CSS3和Canves)
2016/09/15 Javascript
Node.js自定义实现文件路由功能
2017/09/22 Javascript
swiper动态改变滑动内容的实现方法
2018/01/17 Javascript
详解Vue之计算属性
2020/06/20 Javascript
vue.js实现h5机器人聊天(测试版)
2020/07/16 Javascript
[02:40]DOTA2英雄基础教程 巨牙海民
2013/12/23 DOTA
[16:01]夜魇凡尔赛茶话会 第二期01:你比划我猜
2021/03/11 DOTA
python实现查找两个字符串中相同字符并输出的方法
2015/07/11 Python
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
2018/06/06 Python
Python爬虫包BeautifulSoup学习实例(五)
2018/06/17 Python
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2018/12/26 Python
pandas DataFrame的修改方法(值、列、索引)
2019/08/02 Python
python 利用jinja2模板生成html代码实例
2019/10/10 Python
基于python实现文件加密功能
2020/01/06 Python
Python 简单计算要求形状面积的实例
2020/01/18 Python
html5 Canvas实现图片旋转的示例
2018/01/15 HTML / CSS
高中三年学习生活的自我评价
2013/10/10 职场文书
2015年公务员个人工作总结
2015/04/24 职场文书
2016教师年度考核评语大全
2015/12/01 职场文书
SpringBoot生成License的实现示例
2021/06/16 Java/Android
详细聊聊vue中组件的props属性
2021/11/02 Vue.js
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
2022/04/29 Servers
深入理解pytorch库的dockerfile
2022/06/10 Python