京公网安备 11010802034615号
经营许可证编号:京B2-20210330
Python列表和元组的定义与使用操作示例
本文实例讲述了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]
运行结果:
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
做产品立项、写商业计划书、规划市场进入策略时,"市场调研"和"竞品分析"几乎总是被绑在一起说,甚至被当成同一件事。但实际 ...
2026-11-19 在做数据治理、搭数据中台或者梳理数据库表结构时,"主数据"和"参考数据"是被提及频率最高、也最容易被混为一谈的两个概念。 ...
2026-11-19“纲举目张,执本末从。”若想在数据分析领域有所收获,一套合适的学习教材至关重要。一套优质且契合需求的学习教材无疑是那关键 ...
2026-11-19一、什么是本体 在知识图谱和语义建模里,本体(Ontology)是对一个领域内概念、属性、关系和实例的形式化描述。它回答三个问题 ...
2026-11-18一、K-S 曲线解决什么问题 风控评分卡、客户流失模型、欺诈识别模型建好之后,最核心的一个问题是:模型能不能把"会变坏"和"不会 ...
2026-11-18核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-18核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-18产品上线之后,真正回答"用户是谁、想要什么、卡在哪里"的,往往不是调研问卷,而是用户留下的一条条行为记录。点击、浏览、停留 ...
2026-11-17在日常数据库开发与报表查询中,日期格式几乎是最高频的"打交道"对象:页面上要展示 yyyy-MM-dd,接口要返回 ISO 8601 字符串, ...
2026-11-17核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-17一、销售行业为什么离不开大数据分析 销售行业的本质是"把合适的产品,在合适的时间,卖给合适的人,并持续复购"。传统销售靠经 ...
2026-11-16一、什么是频数分布 拿到一批连续数据(员工年龄、商品价格、考试分数、订单金额),最想先看的往往不是平均值,而是数据集中在 ...
2026-11-16教育部办公厅关于成立全国数据职业教育教学指导委员会(2026—2030年)的通知 教职成厅函〔2026〕25号 各省、自治区、直辖市教 ...
2026-11-16CDA数据分析师 出品 作者:李诗怡 基础与单表查询 四大类语言 · 数据库基础 · DQL 单表查询 ” 一、SQL 四大类语言与数据 ...
2026-11-13核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-13一、问题从哪来 财务分析里算增长率,常规公式所有人都会写: 增长率本年数上年数上年数 这个公式在"上年为正"时完全成立。但一 ...
2026-11-12一、什么是加权聚类 普通聚类默认"每个样本、每个指标都同等重要":距离公式里每个样本权重都是 1,每个指标在距离计算里占比相 ...
2026-11-12“纲举目张,执本末从。”若想在数据分析领域有所收获,一套合适的学习教材至关重要。一套优质且契合需求的学习教材无疑是那关键 ...
2026-11-12一、需求从哪来 做 Excel 报表时,"从一个透视表取数据"这个需求通常有三种来源: 汇总页引用:明细透视表在另一个 sheet,汇总 ...
2026-11-11一、透视表的价值在哪 一句话概括:数据透视表把"写公式汇总"变成了"拖字段看结果"。 传统做法要写 SUMIFS、COUNTIFS,改一个维 ...
2026-11-11