您的位置:首页 > 编程语言 > Python开发

3.9.1 - Lists in Python

2015-11-18 03:13 561 查看
# Python List Examples

a = [] #Empty List

b = [0, 1, 2, 3, 4] #List with 5 items

c = [3.14, "abc", [1, 2, 3, 4]]  #List with a nested list

d = list('Python')  # List of iterable items

e = list(range(0, 10))  # List of integers from 0-9

len(b)
len(c[2])

b + c
c + b

b * 4

3.14 in c

b[2] = 9 # mutable

temp_string = "Python"

temp_string[2] = "a" # error inmutable

c[2][0]

c[2][-1]

b[1:]
b[:2]
b[1:3]


运行结果如下:

a = [] #Empty List
b = [0, 1, 2, 3, 4] #List with 5 items
c = [3.14, "abc", [1, 2, 3, 4]] #List with a nested list
d = list('Python') # List of iterable items

d
// Result: ['P', 'y', 't', 'h', 'o', 'n'] //
e = list(range(0, 10)) # List of integers from 0-9

e
// Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] //
len(b)
// Result: 5 //
len(c[2])
// Result: 4 //
b + c
// Result: [0, 1, 2, 3, 4, 3.14, 'abc', [1, 2, 3, 4]] //
c + b
// Result: [3.14, 'abc', [1, 2, 3, 4], 0, 1, 2, 3, 4] //
b * 4
// Result: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] //
3.14 in c
// Result: True //
b[2] = 9 # mutable
b
// Result: [0, 1, 9, 3, 4] //
c[2][0]
// Result: 1 //
c[2][-1]
// Result: 4 //
b[1:]
// Result: [1, 9, 3, 4] //
b[:2]
// Result: [0, 1] //
b[1:3]
// Result: [1, 9] //
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: