• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    Python3.6.5中如何使用tuple這種數據類型

    python是一種簡單易學且功能壯大的編程說話,它具有白話化的釀成體例,深受大師喜愛。跟著人工智能的成長,python越來越受大師喜愛。在利用python編程的過程中我們總會碰到各類各樣的問題,可是我們總會找到解決的方案的。例如下面的這個問題。

    東西/原料

    • 電腦

    方式/步調

    1. 1

      若何建立tuple這種數據類型。a=(1,2,3)就可以建立一個tuple數據了,list數據是a=[1,2,3].

    2. 2

      界說空的tuple和只有一個元素的tuple。a=()   a=(1,) 因為()既可以暗示建立tuple,也是一種運算符,所以,界說只有一個元素的tuple時必需在元素后面加一個‘,’來消弭歧義。

      >>> b=()

      >>> b

      ()

      >>> b=(1)

      >>> b

      1

      >>> b=(1,)

      >>> b

      (1,)

      >>> 

    3. 3

      tuple數據中的元素也可所以一個tuple,也可所以字符串或者整數。

    4. 4

      若何挪用tuple里的一個原始呢?和list中一樣,用a[0]  而不是a(0),圓括號是挪用函數的方式。

      >>> a=(1,2,3)

      >>> a

      (1, 2, 3)

      >>> a(0)

      Traceback (most recent call last):

        File "<pyshell#3>", line 1, in <module>

          a(0)

      TypeError: 'tuple' object is not callable

      >>> a[0]

      1

      >>> a[2]

      3

    5. 5

      tuple和list長短常相似的數據類型,不同是tuple建立了今后此中的元素不克不及點竄。

      >>> a[0]=2

      Traceback (most recent call last):

        File "<pyshell#6>", line 1, in <module>

          a[0]=2

      TypeError: 'tuple' object does not support item assignment

    6. 6

      因為tuple不克不及點竄,所以它沒有list的.append() .pop()  .insert() 等屬性。

      >>> a.append(4)

      Traceback (most recent call last):

        File "<pyshell#20>", line 1, in <module>

          a.append(4)

      AttributeError: 'tuple' object has no attribute 'append'

      >>> a.pop()

      Traceback (most recent call last):

        File "<pyshell#21>", line 1, in <module>

          a.pop()

      AttributeError: 'tuple' object has no attribute 'pop'

      >>> a.insert(1,9)

      Traceback (most recent call last):

        File "<pyshell#22>", line 1, in <module>

          a.insert(1,9)

      AttributeError: 'tuple' object has no attribute 'insert'

      >>> 

    注重事項

    • 若是感覺這篇經驗幫到了你,請投票、點贊鼓動勉勵我哦!感謝啦! 若是有任何疑問,send me a message,please!
    • 發表于 2018-06-05 00:00
    • 閱讀 ( 1027 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆