# 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] //