[python]python列表、元组

1. 列表和元组简介

列表:用中括号[]包裹,元素的个数及元素的值可以改变。

元组:用小括号()包裹,不可用更改。

通过切片运算[]和[:]可以得到子集。

2.列表

示例:

List = [1, 2, 3, 4]
print List
print List[0]
print List[1:3]
List[3] = 5
print List

运行结果:

[1, 2, 3, 4]
1
[2, 3]
[1, 2, 3, 5]

3. 元组

示例:

Tuple = ('Today', 'is', '11', '20')
print Tuple
print Tuple[0]
print Tuple[0:2]
Tuple[1] = 'are'
print Tuple

运行结果:

Traceback (most recent call last):
  File "C:UsersAdministratorworkspacepracticesrcpractice_testdailytest.py", line 5, in <module>
('Today', 'is', '11', '20')
Today
('Today', 'is')
    Tuple[1] = 'are'
TypeError: 'tuple' object does not support item assignment
原文地址:https://www.cnblogs.com/sophia194910/p/4997986.html