前言
什麼是元組?
元組的使用與列表相似,不同之處在於元組是不可修改的,元組使用圓括號( ),而列表使用中括弧[ ]。
大綱
- 定義元組
- 常用操作
一.定義元組
1. 使用逗號的方法:
https://gist.github.com/Celia-code/0258748256705e006d27f2b8cd75129a
2. 使用圓括號的方法:
https://gist.github.com/Celia-code/41a5d8e11450757f92269a2c0ad49fb2
3. 使用tuple函數:
範例:將一列表轉換為元組
https://gist.github.com/Celia-code/e36efa69ab3bf26aa5e85512a9a88439
4. 定義只有一個元素的元組:
https://gist.github.com/Celia-code/0680526035bc684d07e862ba0ba03b63
二.常用操作
1. 下標操作:和列表一樣,均使用索引值來獲取相對應的元素
https://gist.github.com/Celia-code/a469394dfe93deb082dd2f69c99a00e6
2.切片操作:跟列表與字符串的切片操作一樣
https://gist.github.com/Celia-code/b1224e474830a79e1eaf7bc571f49dfc
3.解組操作:拆解元組並自定義變量來接收元組內相對應的值
https://gist.github.com/Celia-code/1d15a38e6a0b9c6de6efcc6219fc4a5e
??若只想要元組中的某個值,不需要所有的值,可以通過_來作為省略:
https://gist.github.com/Celia-code/bfdef4614c70db8dfea44298bbd0bcaf
4.count方法:獲取元組中某個值出現的次數,跟列表中的用法相同
https://gist.github.com/Celia-code/0dfaf6568870226d8b22ab47d5df254c
5.index方法:獲取元組中某個值的下標,跟列表中的用法相同
https://gist.github.com/Celia-code/134effa5cf2dca0b88645b2eeecc1d58
三.相關連結
列表介紹 ??Python常用數據類型 – 列表







