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访问系统环境变量的方法
Apr 29 Python
Python单链表简单实现代码
Apr 27 Python
python学习 流程控制语句详解
Jun 01 Python
Python学习小技巧之利用字典的默认行为
May 20 Python
python 实现A*算法的示例代码
Aug 13 Python
Python实战之制作天气查询软件
May 14 Python
django中使用Celery 布式任务队列过程详解
Jul 29 Python
在Python中os.fork()产生子进程的例子
Aug 08 Python
python爬取王者荣耀全皮肤的简单实现代码
Jan 31 Python
python3 logging日志封装实例
Apr 08 Python
浏览器常用基本操作之python3+selenium4自动化测试(基础篇3)
May 21 Python
基于Python和openCV实现图像的全景拼接详细步骤
Oct 05 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获取远程图片并把它保存到本地的代码
2008/04/07 PHP
php 类自动载入的方法
2015/06/03 PHP
PHP中new static()与new self()的比较
2016/08/19 PHP
php redis实现文章发布系统(用户投票系统)
2017/03/04 PHP
laravel高级的Join语法详解以及使用Join多个条件
2019/10/16 PHP
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
2011/05/24 Javascript
javascript截取字符串(通过substring实现并支持中英文混合)
2013/06/24 Javascript
jquery操作select方法汇总
2015/02/05 Javascript
jQuery获取标签文本内容和html内容的方法
2015/03/27 Javascript
js实现简单选项卡与自动切换效果的方法
2015/04/10 Javascript
JavaScript继承模式粗探
2016/01/12 Javascript
通过npm引用的vue组件使用详解
2017/03/02 Javascript
Angularjs根据json文件动态生成路由状态的实现方法
2017/04/17 Javascript
vue-router 起步步骤详解
2019/03/26 Javascript
vue中注册自定义的全局js方法
2019/11/15 Javascript
jquery实现进度条状态展示
2020/03/26 jQuery
详谈Python2.6和Python3.0中对除法操作的异同
2017/04/28 Python
tensorflow实现KNN识别MNIST
2018/03/12 Python
利用Python实现原创工具的Logo与Help
2018/12/03 Python
Python numpy中矩阵的基本用法汇总
2019/02/12 Python
Python基础之循环语句用法示例【for、while循环】
2019/03/23 Python
python 实现兔子生兔子示例
2019/11/21 Python
如何用Python提取10000份log中的产品信息
2021/01/14 Python
美国鞋类购物网站:Shiekh Shoes
2016/08/21 全球购物
波兰灯具、照明和LED购物网站:Lampy.pl
2019/03/11 全球购物
使用索引(Index)有哪些需要考虑的因素
2016/10/19 面试题
护士辞职信范文
2014/01/19 职场文书
小学信息技术教学反思
2014/02/10 职场文书
竞选大队长演讲稿
2014/04/29 职场文书
供应链金融服务方案
2014/05/25 职场文书
法定代表人授权委托书范文
2014/08/02 职场文书
2014年检验科工作总结
2014/11/22 职场文书
2014年小学教研工作总结
2014/12/06 职场文书
2015法院个人工作总结范文
2015/05/25 职场文书
导游词之任弼时故居
2020/01/07 职场文书
Navicat Premium自定义 sql 标签的创建方式
2022/09/23 数据库