元祖 列表 字典 的用法:

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

  • 元祖:
  • 列表:
  • 字典:

猜你喜欢

转载自www.cnblogs.com/huqi-denghong/p/9934308.html