> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter1notes-from-research/chapter-2-write-python-code/39sequence.md).

# 3.6.Sequence

* 序列
  * python共有六種內建的序列: list, tuple, string, unicode, buffer, xrange
    * List
      * 堆疊是從0開始
      * 可放入**混合型態**的資料
    * Tuple
      * 不可改變的list (immutable)
    * Dictionary
* 切片操作
  * 規則:
    * 1.con\[start\_index]：
      * 返回索引值為start\_index的對象. start\_index為-len(con)到len(con)-1之間任意整數
    * 2.con\[start\_index: end\_index]：
      * 返回索引值為start\_index到end\_index－1之間的連續對象 (不包括end\_index)
    * 3.con\[start\_index: end\_index : step]：
      * 返回索引值為start\_index到end\_index-1之間，並且索引值與start\_index之差可以被step整除的連續對象
    * 4.con\[start\_index: ]：
      * 缺省end\_index，表示從start\_index開始到序列中最後一個對象
    * 5.con\[: end\_index]：
      * 缺省start\_index，表示從序列中第一個對象到end\_index-1之間的片段
    * 6.con\[:]：
      * 缺省start\_index和end\_index，表示從第一個對象到最後一個對象的完整片段
    * 7.con\[::step]：
      * 缺省start\_index和end\_index，表示對整個序列按照索引可以被step整除的規則取值
    * 舉例來說:

      ```
        a = 'Iloveyou'
        a[5]
        # 印出'y'

        a[1:5]
        # 印出'love'

        a[1:5:2]
        # 印出'lv'

        a[5:]
        # 印出'you'

        a[:5]
        # 印出'Ilove'

        a[:]
        # 印出'Iloveyou'

        a[::2]
        # 印出'Ioeo'
      ```
