2019-01-06Python中的tuple和list用法区别
tuple是另一种有序的列表,中文翻译为“ 元组 ”。tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改了。
创建tuple和创建list唯一不同之处是用( )替代了[ ]。
tuple没有 append()方法,也没有insert()和pop()方法。
获取 tuple 元素的方式和 list 是一模一样的,我们可以正常使用 t[0],t[-1]等索引方式访问元素,但是不能赋值成别的元素。
用()定义单元素的tuple有歧义,所以 Python 规定,单元素 tuple 要多加一个逗号“,”,这样就避免了歧义:
t=(1) print t
结果是:1
t=(1,) print t
结果是:(1,)
多元素 tuple 加不加这个额外的“,”效果是一样的。
相关文章
- Python报错:SyntaxError: Missing parentheses in call to 'print'
- 一篇文章入门Python
- Python入门经典编程题2
- Python中移动函数
- anaconda安装好了只有anaconda prompt,而没有Spyder,Anaconda Navigator等等
- Python如何在一个文本的不同行前加入序号?
- Python中with open语句写入文件时显示“io.UnsupportedOperation: not writable”
- Python入门经典编程题1
- Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案
- Python 3.6.0 Shell中print时候出现SyntaxError: invalid syntax解决方法
暂无留言,快抢沙发!