Tee

最後修改於 2025 / 5 / 7 by CML

lst2 = [1, 2, 3, 4, 5]
print('Lst2', lst2)
print('Len(Lst2) = ', len(lst2))
print('Lst2[0] = ', lst2[0])
print('Lst2[3] = ', lst2[3])
print('Lst2[-1] = ', lst2[-1])

print('Lst2[0:3] = ', lst2[0:3])
print('Lst2[2:4] = ', lst2[2:4])
print('Lst2[-3:-1] = ', lst2[-3:-1])

lst2.append(10)
print('Lst2 append 10', lst2)
lst2.append(15)
print('Lst2 append 15', lst2)
lst2.insert(1, 11)
print('Lst2 insert 11', lst2)
lst2 = [1, 2, 3, 4, 5]
print('Lst2', lst2)
print('Len(Lst2) = ', len(lst2))

# 查詢 list 串列中某個索引值 (start)
index1 = eval(input('請輸入要查詢的索引值<start>:'))
print('Lst2[%d] = %d' % (index1, lst2[index1]))

# 查詢 list 串列中某個索引值 (end)
index2 = eval(input('請輸入要查詢的索引值<end>:'))
print('Lst2[%d] = %d' % (index2, lst2[index2]))

# list append 新的值
value1 = eval(input('請輸入人要新增增加在 list 中的值:'))
lst2.append(value1)
print('Lst2 append %d', value1)
print('Lst2', lst2)

# list 指定索引值 insert value
value2, value3 = eval(input('請輸入人要新增在 list 中的索引值及值:'))
lst2.insert(value2, value3)
print('Lst2 在 %d insert %d' % (value2, value3))
print('Lst2', lst2)
lst2 = [1, 2, 3, 4, 5]
print('Lst2:', lst2)
print('len(Lst2)=', len(lst2))

# 查詢 list 串列中某個索引值 (start)
index1 = eval(input('請輸入要查詢的索引值<start>:'))
print('Lst2[%d] = %d' % (index1, lst2[index1]))

# 查詢 list 串列中某個索引值 (end)
index2 = eval(input('請輸入要查詢的索引值<end>:'))
print('Lst2[%d] = %d' % (index2, lst2[index2]))

# list iterable
for i in range(2):
print(f'\n第{i + 1}輪查詢:')
index3, index4 = eval(input('請輸入要查詢的 list 串列索引範圍(用逗號分隔,如 2, 5):'))
if index3 < index4: # 正確
result = lst2[index3:index4]
print(f'Lst2[{index3}:{index4}] = {result}')
else: # 錯誤
result = lst2[index4:index3]
print(f'Lst2[{index4}:{index3}] = {result}')

# list append 新的值
value1 = eval(input('請輸入要新增的值:'))
lst2.append(value1)
print('Lst2 append %d' % value1)
print('Lst2', lst2)

# list 指定索引值 insert value
value2, value3 = eval(input('請輸入要新增在 List 中的索引值和值(用逗號分隔):'))
lst2.insert(value2, value3)
print('Lst2 在 %d insert %d' % (value2, value3))
print('Lst2', lst2)

# list pop 結果
lst2.pop()
print('lst2 pop 後 ', lst2)

# list pop 指定索引值結果
index5 = eval(input('請輸入 list 中要 pop 的索引值:'))
lst2.pop(index5)
print(f'lst2 pop 索引[{index5}]後的內容 = {lst2}')

# list 串列中 remove 某個 value
index5 = eval(input('請輸入 list 中要 remove 的值:'))
lst2.remove(index5)
print(f'lst2 remove 的值:{index5}, remove 後的內容 = {lst2}')

print('len(lst2) = ', len(lst2))

lst2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print('Lst2', lst2)
print('Len(Lst2) = ', len(lst2))
print('Lst2[0] = ', lst2[0])
print('Lst2[3] = ', lst2[3])
print('Lst2[-1] = ', lst2[-1])

print('Lst2[0:3] = ', lst2[0:3])
print('Lst2[2:4] = ', lst2[2:4])
print('Lst2[-3:-1] = ', lst2[-3:-1])

lst2.append(10)
print('Lst2 append 10', lst2)
lst2.append(15)
print('Lst2 append 15', lst2)
lst2.insert(1, 11)
print('Lst2 在 1 insert 11', lst2)
print('Len(Lst2) = ', len(lst2))

lst2.pop()
print('Lst2 pop 後', lst2)
lst2.pop(1)
print('Lst2 pop 移除索引/1內容', lst2)
lst2.remove(3)
print('Lst2 remove 3', lst2)
print('Len(Lst2) = ', len(lst2))
lst1 = [1, 5, 3, 7]
print('lst1', lst1)
lst1.reverse()
print('lst1 reverse: ', lst1)
lst1.sort()
print('lst1 sort: ', lst1)
lst1.reverse()
print('lst1', lst1)

print('5 in lst1:', 5 in lst1)
print('5 not in lst1:', 5 not in lst1)

print('8 in lst1:', 8 in lst1)
print('8 not in lst1:', 8 not in lst1)

print('sum(lst1) = ', sum(lst1))
print('max(lst1) = ', max(lst1))
print('min(lst1) = ', min(lst1))

lst1 = [1, 5, 3, 7]
lst2 = [8, 9]
print('lst1 + lst2 = ', lst1 + lst2)
print('lst2 * 5 = ', lst2 * 5)
print('2 * lst2 = ', 2 * lst2)
返回頂端