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从零实现贝叶斯分类器的机器学习的教程
Mar 31 Python
使用python实现链表操作
Jan 26 Python
python微元法计算函数曲线长度的方法
Nov 08 Python
详解python中init方法和随机数方法
Mar 13 Python
pyqt5 tablewidget 利用线程动态刷新数据的方法
Jun 17 Python
django 快速启动数据库客户端程序的方法示例
Aug 16 Python
python 表格打印代码实例解析
Oct 12 Python
pyecharts调整图例与各板块的位置间距实例
May 16 Python
Python 实现自动登录+点击+滑动验证功能
Jun 10 Python
python爬虫线程池案例详解(梨视频短视频爬取)
Feb 20 Python
python 中的@运算符使用
May 26 Python
Python实现抖音热搜定时爬取功能
Mar 16 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/22 PHP
PHP开发过程中常用函数收藏
2009/12/14 PHP
PHP 之Section与Cookie使用总结
2012/09/14 PHP
CI框架AR数据库操作常用函数总结
2016/11/21 PHP
PHP基于自增数据如何生成不重复的随机数示例
2017/05/19 PHP
Javascript Tab 导航插件 (23个)
2009/06/11 Javascript
javascript-简单的计算器实现步骤分解(附图)
2013/05/30 Javascript
js中复制行和删除行的操作实例
2013/06/25 Javascript
jquery中event对象属性与方法小结
2013/12/18 Javascript
JS解析XML文件和XML字符串详解
2015/04/17 Javascript
js不间断滚动的简单实现
2016/06/03 Javascript
浅谈js图片前端预览之filereader和window.URL.createObjectURL
2016/06/30 Javascript
JavaScript中最容易混淆的作用域、提升、闭包知识详解(推荐)
2016/09/05 Javascript
Javascript生成带参数的二维码示例
2016/10/10 Javascript
微信小程序实战之自定义模态弹窗(8)
2017/04/18 Javascript
vue限制输入框只能输入8位整数和2位小数的代码
2019/11/06 Javascript
vue开发中遇到的问题总结
2020/04/07 Javascript
javascript设计模式 ? 代理模式原理与用法实例分析
2020/04/16 Javascript
[01:14]DOTA2亚洲邀请赛小组赛赛前花絮
2017/03/27 DOTA
[01:46]2020完美世界全国高校联赛秋季赛报名开启
2020/10/15 DOTA
python3.3使用tkinter开发猜数字游戏示例
2014/03/14 Python
从源码解析Python的Flask框架中request对象的用法
2016/06/02 Python
opencv python统计及绘制直方图的方法
2019/01/21 Python
python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例
2020/03/01 Python
解决Tensorflow2.0 tf.keras.Model.load_weights() 报错处理问题
2020/06/12 Python
keras实现theano和tensorflow训练的模型相互转换
2020/06/19 Python
安装python依赖包psycopg2来调用postgresql的操作
2021/01/01 Python
c++工程师面试问题
2013/08/04 面试题
珍惜水资源建议书
2014/03/12 职场文书
《每逢佳节倍思亲》教后反思
2014/04/19 职场文书
“九一八事变纪念日”国旗下讲话稿
2014/09/14 职场文书
2014党员干部四风问题对照检查材料思想汇报
2014/09/24 职场文书
苦儿流浪记读书笔记
2015/07/01 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
redis配置文件中常用配置详解
2021/04/14 Redis
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python