Python入門習題大全 切片

2021-10-04 20:57:00 字數 895 閱讀 4987

隨意建立乙個列表,在末尾新增幾行**,以完成如下任務。

列印訊息「the first three items in the list ate:」,再使用切片來列印列表的前三個元素。

列印訊息「three items from the middle of the list ate:」,再使用切片來列印列中間的三個元素。

列印訊息「the last three items in the list ate:」,再使用切片來列印列表的末尾的三個元素。

# 切片

num =

['a'

,'b'

,'c'

,'d'

,'e'

]print

("the first three items in the list are:"

)print

(num[:3

])print

("three items from the middle of the list are:"

)print

(num[1:

4])print

("the last three items in the list are:"

)print

(num[-3

:])

輸出為:

the first three items in the list are:

['a', 'b', 'c']

three items from the middle of the list are:

['b', 'c', 'd']

the last three items in the list are:

['c', 'd', 'e']

Python入門習題大全 比薩

想出至少三種你喜歡的比薩,將其名稱儲存在乙個列表中,再使用 for 迴圈將每種比薩的名稱都列印出來。1.修改這個 for 迴圈,使其列印包含比薩名稱的句子,而不僅僅是比薩的名稱。對於每種比薩,都顯示一行輸出,如 i like pepperoni pizza 2.在程式末尾新增一行 它不在 for 迴...

Python入門習題大全 序數

序數表示位置,如 1st 和 2nd。大多數序數都以 th 結尾,只有 1 2 和 3 例外。在乙個列表中儲存數字 1 9。遍歷這個列表。在迴圈中使用乙個 if elif else 結構,以列印每個數字對應的序數。輸出內容應為 1st 2nd 3rd 4th 5th 6th 7th 8th 和 9t...

Python入門習題大全 汽車

編寫乙個函式,將一輛汽車的資訊儲存在乙個字典中。這個函式總是接受製造商和型號,還接受任意數量的關鍵字實參。這樣呼叫這個函式 提供必不可少的資訊,以及兩個名稱值對,如顏色和選裝配件。這個函式必須能夠像下面這樣進行呼叫 car make car subaru outback color blue tow...