Python之路,第六篇:Python入門與基礎6

來源:互聯網
上載者:User

標籤:不能   trace   運算子   file   seq   變數   recent   type   初始化   

 python 列表

           序列類型簡介(sequence)

                          字串str
                         列表list
                         元祖tuple

           概念:

                     列表是由一系列特定元素組成的,元素之間可能沒有任何關聯,但是他們之間有先後循序關聯性
                     列表可以改變各個元素的值
                     列表是一個容器。

           空列表: L = [ ]  # 空列表

                          L = list()  # 空列表,是一個函數

           建立一個非空列表:

                         L = [1,2,3,4,5]

           列表的產生函數list():

                        list()產生一個空列表等同於[ ]  ;

                        list(iterable)     用一個可迭代對象初始化列表

                       例子:

1 >>> l=list(range(11))2 >>> l3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]4 >>> 5 >>> s=‘abcdefg‘6 >>> l=list(s)7 >>> l8 [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]9 >>> 
list1

          列表的運算子

                    +     、 +=    、  *  、 *=

                 說明: + 號運算子用於拼接列表;  += 運算子用於原來列表與右側列表拼接產生的新列表(x = x+y 等同於 x +=y)

                           * 號運算子用於產生重複的列表 ;   * =號運算子用於原列表產生重複的列表,並改變變數的綁定;

 1 >>> x=[1,2,3] 2 >>> y=[4,5,6] 3 >>> z = x + y 4 >>> z 5 [1, 2, 3, 4, 5, 6] 6 >>> len(z) 7 6 8 >>> m = y + x   9 >>> m10 [4, 5, 6, 1, 2, 3]  11 >>> len(m)12 613 >>> 
+號拼接
1 >>> x=[1,2,3]2 >>> y=[4,5,6]3 >>> x += y4 >>> x5 [1, 2, 3, 4, 5, 6]6 >>> y7 [4, 5, 6]8 >>> 
+=號拼接
1 >>> [1,2] * 32 [1, 2, 1, 2, 1, 2]3 >>> 3 * [1,2]4 [1, 2, 1, 2, 1, 2]5 >>> 
*
1 >>> x = [1,2,3]2 >>> x *= 43 >>> x4 [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]5 >>> 
*=

         列表的關係(比較)運算子:

                 >     >=    <     <=    ==    !=

 1 >>> x = [1,2,3] 2 >>> y = [2,3,4] 3 >>> x != y 4 True 5 >>> x > y 6 False 7 >>> x < y 8 True 9 >>> x == y10 False11 >>> [1,2,3] == [3,2,1]12 False13 >>> [1,‘two‘] < [‘two‘,1]14 Traceback (most recent call last):15   File "<pyshell#59>", line 1, in <module>16     [1,‘two‘] < [‘two‘,1]17 TypeError: unorderable types: int() < str()18 >>>#字串和列表是不能比較的
View Code

 

 

 

 

          

Python之路,第六篇:Python入門與基礎6

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.