1. 程式人生 > >元祖 列表 字典 的用法:

元祖 列表 字典 的用法:

1索引

索引是從0開始計數;當索引值為負數時,表示從最後一個元素(從右到左)開始計數。這裡列舉幾個例子:

#字串字面值可以直接使用索引,不需要專門的變數引用

>>> 'Hello World!'[0]

'H'

>>> 'Hello World!'[11]

'!'

#注意越界

>>> 'Hello World!'[12]

Traceback (most recent call last):

File "<stdin>", line 1, in <module> IndexError: string index out of range

#表示右邊第一個

>>> 'Hello World!'[-1]

'!'

>>> 'Hello World!'[-11]

'e'

#注意越界

>>> 'Hello World!'[-12]

'H'

>>> 'Hello World!'[-13] Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: string index out of range

 

  • 元祖:

 

 

 

  • 列表:

 

 

 

  • 字典: