切片本身就是对象,可以使用内置slice()函数存储在变量中。切片变量可用于使您的代码更具可读性并促进重用。
>>> programmer_1 = [ 1956, 'Guido', 'van Rossum', 'Python', 'Netherlands'] >>> programmer_2 = [ 1815, 'Ada', 'Lovelace', 'Analytical Engine', 'England'] >>> name_columns = slice(1, 3) >>> programmer_1[name_columns] ['Guido', 'van Rossum'] >>> programmer_2[name_columns] ['Ada', 'Lovelace']