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]
编写一个python程序
打印消息“The first three items in the list are”,再使用切片来打印列表的前三个元素,中间三个元素,末尾三个元素。

原文地址: https://www.cveoy.top/t/topic/tvS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录