PythonStudy——列表的常用操作 List of common operations
# 1.索引取值: 列表名[index]
s1 = [1, 3, 2] print(s1[0]) print(s1[-1])
# 2.列表运算: 得到的是新list
s2 = [1, 2, 3] print(s2 + s2) print(s2 * 2) print(s2)
# 3.list的长度
s3 = [3, 4, 1, 2, 5] print(len(s3))
# 4.切片:[start_index:end_index:step]
s4 = [3, 4, 1, 2, 5] new_s4 = s4[::-1] print(new_s4) new_s4 = s4[1:4:] print(new_s4) new_s4 = s4[-2:-5:-1] print(new_s4)
# 5.成员运算:in
s5 = [3, 4, '1', 2, 5] print('1' in s5) print(1 in s5) print(5 not in s5)
# 6.循环
for v in s5: print(v, type(v))
# 只打印数字类型的数据
for v in s5: if isinstance(v, int): print(v, end=' ')