3.9.1 - Lists in Python

来源:互联网 发布:网上开淘宝店流程 编辑:程序博客网 时间:2024/05/21 13:55
# Python List Examplesa = [] #Empty Listb = [0, 1, 2, 3, 4] #List with 5 itemsc = [3.14, "abc", [1, 2, 3, 4]]  #List with a nested listd = list('Python')  # List of iterable itemse = list(range(0, 10))  # List of integers from 0-9len(b)len(c[2])b + cc + bb * 43.14 in cb[2] = 9 # mutabletemp_string = "Python"temp_string[2] = "a" # error inmutablec[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] //



0 0
原创粉丝点击