元組是不可變的列表,也就是說元組的值不能夠修改,所以元組通常只用於只存不取的需求
python
語族和列表同樣能夠用來存儲多個愛好,多種描述狀態等
app
在()內能夠又多個任意來行的值,用逗號分隔開元素
code
hobby_list = ('ran','play','read')
索引
1.索引取值
內存
2.切片
class
3.長度
循環
4.成員運算in/not in
數據類型
5.循環
方法
6.count
數據
7.index
food_tuple = ('apple','banana','orange','grape') print(food_tuple[0]) # apple
food_tuple = ('apple','banana','orange','grape') print(food_tuple[:-1]) # ('apple', 'banana', 'orange') print(food_tuple[::-1]) # ('grape', 'orange', 'banana', 'apple')
food_tuple = ('apple','banana','orange','grape') print(len(food_tuple)) # 4
food_tuple = ('apple','banana','orange','grape') print('app' in food_tuple) # False print('apple' in food_tuple) # True
food_tuple = ('apple','banana','orange','grape') for food in food_tuple: print(food) # 結果 apple banana orange grape
food_tuple = ('apple','banana','orange','grape','apple') print(food_tuple.count('apple')) # 2
food_tuple = ('apple','banana','orange','grape','apple') print(food_tuple.index('banana')) # 0 print(food_tuple.index('apple')) # 1
name_tuple = ('xichen') print(id(name_tuple)) # 2474398778232 name_tuple = ('eee') print(id(name_tuple)) # 2474398778288
1.列表可變的緣由是:索引所對應的值的內存地址是能夠改變的
2.元組不可變得緣由是:索引所對應的值的內存地址是不能夠改變的