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列表与元组详解实例
Nov 01 Python
itchat和matplotlib的结合使用爬取微信信息的实例
Aug 25 Python
在Python中实现替换字符串中的子串的示例
Oct 31 Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
Jun 18 Python
python替换字符串中的子串图文步骤
Jun 19 Python
opencv实现简单人脸识别
Feb 19 Python
Python运行提示缺少模块问题解决方案
Apr 02 Python
关于Keras Dense层整理
May 21 Python
利用Vscode进行Python开发环境配置的步骤
Jun 22 Python
python实现邮件循环自动发件功能
Sep 11 Python
python常量折叠基础知识点讲解
Feb 28 Python
Python爬虫之爬取最新更新的小说网站
May 06 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
简单介绍下 PHP5 中引入的 MYSQLI的用途
2007/03/19 PHP
PHP获取php,mysql,apche的版本信息示例代码
2014/01/16 PHP
简单谈谈php浮点数精确运算
2016/03/10 PHP
laravel实现按时间日期进行分组统计方法示例
2019/03/23 PHP
PHP基础之输出缓冲区基本概念、原理分析
2019/06/19 PHP
ExtJs 表单提交登陆实现代码
2010/08/19 Javascript
Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
2010/08/27 Javascript
js Array操作的最简短最容易理解方法
2013/12/09 Javascript
详解js中的apply与call的用法
2016/07/30 Javascript
jQuery基于BootStrap样式实现无限极地区联动
2016/08/26 Javascript
Knockout结合Bootstrap创建动态UI实现产品列表管理
2016/09/14 Javascript
javaScript产生随机数的用法小结
2018/04/21 Javascript
微信小程序事件 bindtap bindinput代码实例
2019/08/26 Javascript
[52:20]DOTA2-DPC中国联赛正赛 SAG vs XGBO3 第一场 3月5日
2021/03/11 DOTA
跟老齐学Python之有容乃大的list(3)
2014/09/15 Python
一个基于flask的web应用诞生 flask和mysql相连(4)
2017/04/11 Python
Python实现统计代码行的方法分析
2017/07/12 Python
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
2017/11/02 Python
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
2019/02/13 Python
Python用Try语句捕获异常的实例方法
2019/06/26 Python
Python3并发写文件与Python对比
2019/11/20 Python
Python绘图之二维图与三维图详解
2020/08/04 Python
借助HTML5 Canvas API制作一个简单的猜字游戏
2016/03/25 HTML / CSS
美国知名平价彩妆品牌:e.l.f. Cosmetics
2017/11/20 全球购物
支票、地址标签、包装纸和慰问卡:Current Catalog
2018/01/30 全球购物
Lentiamo荷兰:在线订购隐形眼镜、隐形眼镜液和太阳镜
2019/10/25 全球购物
.NET remoting中对象激活的两种方式
2015/06/08 面试题
vue实现倒计时功能
2021/03/24 Vue.js
临床医学专业个人的自我评价
2013/09/27 职场文书
实习生求职自荐信
2014/02/07 职场文书
大学社团计划书
2014/05/01 职场文书
护士求职信范文
2014/05/24 职场文书
2015年医德考评自我评价
2015/03/03 职场文书
数学教师求职信范文
2015/03/20 职场文书
酒桌上的祝酒词
2015/08/12 职场文书
【海涛教你打DOTA】黑鸟第一视角解说
2022/04/01 DOTA