Python 簡明教程 --- 11,Python 元組

微信公衆號:碼農充電站pro
我的主頁:https://codeshellme.github.iohtml

軟件工程的目標是控制複雜度,而不是增長複雜性。
—— Dr. Pamela Zavepython

目錄git

在這裏插入圖片描述

咱們在上一節介紹了Python 列表list 數據結構,本節來介紹一下元組tuplegithub

1,Python 元組

元組與列表有些類似,它們之間最顯著的不一樣是,元組一旦定義了之後,就不能再修改(增長/刪除其中的元素),而列表能夠被任意的改。shell

Python 元組有以下特色:微信

  • 元組中的元素能夠是任意類型的數據
  • 可以使用下標和切片訪問元組內容
  • 元組一點定義,不能再被修改

2,定義元組

咱們已經知道了定義列表使用中括號[],而定義元組使用小括號() 表示:數據結構

>>> t = ()  # 一個空的元組
>>> t
()
>>> t = ('a', 1, 3.5, True)  # 元組中能夠存聽任意類型
>>> t
('a', 1, 3.5, True)

只有一個元素的元組ssh

當定義的元組中只有一個元素時,須要在元素後邊加個逗號:函數

>>> t = (1,) 
>>> t      
(1,)

若是沒帶逗號,則這個小括號()將不會被認爲是元組符號:編碼

>>> t = (1)   # 至關於沒有帶小括號
>>> t
1
>>> t = ('abc')
>>> t
'abc'

3,元組的大小

可使用len() 來查看一個元組的大小:

>>> t = ('a', 'b', 'c')
>>> len(t)	# 長度爲 3
3
>>> t = (1, 3)
>>> len(t)  # 長度爲 2
2

4,訪問元組

能夠像訪問列表同樣,使用下標切片,和 for 循環來訪問元組。

使用下標訪問元組

>>> t = ('a', 'b', 'c')
>>> t[0]	# 訪問第一個元素
'a'
>>> t[3]    # 下標越界
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> t[-1]   # 訪問倒數第一個元素
'c'
>>> t[-3]   # 訪問倒數第三個元素
'a'
>>> t[-4]   # 下標越界
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

使用切片訪問元組

>>> t = ('a', 'b', 'c')
>>> t[1:3]
('b', 'c')
>>> t[2:]
('c',)
>>> t[:3]
('a', 'b', 'c')
>>> t[:]
('a', 'b', 'c')

遍歷元組

>>> t = ('a', 'b', 'c')
>>> for i in t:
...     print(i)
... 
a
b
c

5,元組不可修改

元組是不可變類型,不能對一個已定義的元組進行如下操做,不然會拋出異常:

  • 添加元素
  • 修改元素
  • 刪除元素

示例:

>>> t = ('a', 'b', 'c')
>>> # 沒有對於元組的添加操做,因此也不用演示 
>>>
>>> t[0] = 1  # 修改元素,拋出異常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
>>> del t[1]  # 刪除元素,拋出異常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

6,元組運算

像列表同樣,元組也能夠進行加運算乘運算in 運算

>>> ('a', 'b') + (1, 2)  # 加運算,獲得一個新的元素
('a', 'b', 1, 2)
>>>
>>> ('a', 'b') * 2       # 乘運算,至關於 n 個元素相加
('a', 'b', 'a', 'b')   
>>>
>>> ('a', 'b') * -1      # n 小於等於 0 時,獲得一個空元組
()
>>> 'a' in ('a', 'b')    # in 運算,判斷一個元素是否包含在元組中
True
>>> 'a' not in ('a', 'b') 
False

7,元組函數

經過dir(tuple) 查看元組支持的方法:

['__add__', '__class__', '__contains__', 
'__delattr__', '__dir__', '__doc__', 
'__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', 
'__getnewargs__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', 
'__lt__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__rmul__', '__setattr__', 
'__sizeof__', '__str__', '__subclasshook__', 
'count', 'index']

能夠看處處了魔法方法,元組類型僅支持兩個方法:

  • count 方法
  • index 方法

且沒有任意一個方法用於修改元組。

1.count 方法

做用:計算元組T 中值爲value 的個數
原型:T.count(value) -> integer
參數:要計算的元素的值
返回值:個數

示例:

>>> t = ['a', 'b', 'c', 'a']
>>> t.count('a')
2
>>> t.count('b')
1
>>> t.count('d')
0

2.index 方法

做用:從元組T[start:stip] 中查找第一個值爲value 的元素
原型:T.index(value, [start, [stop]]) -> integer
參數 value:查找值爲value 的元素
參數 start:元組T 的起始下標
參數 stop:元組T 的終止下標
返回值:若能找到,則返回該元素的下標,不然,拋出ValueError 異常

>>> t = ['a', 'b', 'c']
>>> t.index('b')   # 找到了,返回下標
1
>>> l.index('d')   # 沒找到,拋出 ValueError 異常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'd' is not in list

(完。)


推薦閱讀:

Python 簡明教程 --- 6,Python 控制流

Python 簡明教程 --- 7,Python 字符串

Python 簡明教程 --- 8,Python 字符串函數

Python 簡明教程 --- 9,Python 編碼

Python 簡明教程 --- 10,Python 列表


歡迎關注做者公衆號,獲取更多技術乾貨。

碼農充電站pro

相關文章
相關標籤/搜索