串列像是一排櫃子,可以同時儲存各種型態的資料
購物車 = ['香蕉','檸檬','鳳梨','西瓜',['葡萄','芒果','芭樂']]
用法:[內容1,內容2,內容3.....]
shopping_cart = ['apple', 'book']
print(shopping_cart)
# ['apple', 'book']
print(type(shopping_cart))
# <class 'list'>
shopping_list = ['Good', 5, [True, -5.5]]
print(shopping_list)
# ['Good', 5, [True, -5.5]]
a = ['香蕉', '葡萄', '西瓜']
b = a # 沒有複製
a = ['社會', '自然', '程式']
# a 的項目就改變了
例1.
shopping_cart = ['book', 'juice', 'water']
x = shopping_cart
shopping_cart = ['banana', 'mongo', 'shoes']
print(shopping_cart, x, sep='\n')
# ['banana', 'mongo', 'shoes']
# ['book', 'juice', 'water']
用法:list_1 = list_2[:]
shopping_cart = ['book', 'juice', 'water']
x = shopping_cart[:]
shopping_cart[0] = 'cap'
x[1] = 'egg'
print(shopping_cart)
# ['cap', 'juice', 'water']
print(x)
# ['book', 'egg', 'water']
重點整理: