编写一个python程序 打印消息“The first three items in the list are”,再使用切片来打印列表的前三个元素,中间三个元素,末尾三个元素。
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The first three items in the list are:")
print(my_list[:3])
print("The middle three items in the list are:")
print(my_list[3:6])
print("The last three items in the list are:")
print(my_list[-3:])
输出:
The first three items in the list are:
[1, 2, 3]
The middle three items in the list are:
[4, 5, 6]
The last three items in the list are:
[7, 8, 9]
原文地址: https://www.cveoy.top/t/topic/tvS 著作权归作者所有。请勿转载和采集!