第7類:數組(Tuple)、集合(Set)以及詞典(Dictionary)

TQC+ 程式語言Python 703 數組條件判斷

題目說明:

請撰寫一程式,輸入一些字串至數組(至少輸入五個字串),以字串”end”為結束點(數組中不包含字串”end”)。接著輸出該數組,再分別顯示該數組的第一個元素到第三個元素和倒數三個元素。

範例輸入

president
dean
chair
staff
teacher
student
end

範例輸出

('president', 'dean', 'chair', 'staff', 'teacher', 'student')
('president', 'dean', 'chair')
('staff', 'teacher', 'student')
 

程式碼:

Str = input()
List = []

while Str != 'end':
    List.append(Str)
    Str = input()

Tuple = tuple(List)

print(Tuple)
print(Tuple[0:3])
print(Tuple[len(Tuple) - 3:len(Tuple)])

 

TQC+ 程式語言Python 第7類:數組(Tuple)、集合(Set)以及詞典(Dictionary)





如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝

可以多點幾次喔~~

第一次點讚需使用 Google 或 Facebook 帳號註冊

2 則留言

Ming Tsay 發表迴響取消回覆