久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      Python之元組的學(xué)習(xí)

      一、元組的定義

      1.元組(tuple)

      帶了緊箍咒的列表,元組本身不可變數(shù)據(jù)類型,沒有增刪改查,元組內(nèi)可以存儲任意數(shù)據(jù)類型

      示例【1】

        t = (1, 2, 3, 4)  print t, type(t)

      Python之元組的學(xué)習(xí)

      2、元組的特點

      元組里面包含可變 數(shù)據(jù)類型,可以間接修改元組的內(nèi)容 元組如果只有 一個元素的時候,后面一定要年加逗號,否則數(shù)據(jù)類型不確定

      示例【1】

        t1 = ([1, 2, 3], 4)  print t1[0]  t1[0].append(4)    #給元組索引值為0的元素加一個4進去,因為元組里面包含了可變得數(shù)據(jù)類型,列表    print t1  

      Python之元組的學(xué)習(xí)

      示例【2】:

        t2 = ('hello')    #要是沒有加逗號,引號里面是什么類型就會打印什么類型,而不會打印元組類型  t3 = (1,)         #加逗號才會打印元組類型  print type(t2), type(t3)

      Python之元組的學(xué)習(xí)

      二、元組的特性(索引、切片、重復(fù)、連接、成員操作符)

      1、索引和切片

      示例【1】

        allowUsers = ('root', 'westos', 'fentiao')  print allowUsers[0]      #打印索引值為0的元素,也就是第一個元素  print allowUsers[-1]     #打印倒數(shù)第一個元素  print allowUsers[1:]     #打印除第一個外的其他元素  print allowUsers[2:]     #打印除前兩個之外的其他元素  print allowUsers[:-1]    #打印除最后一個之外的其他元素  print allowUsers[::-1]   #倒敘打印所有元素

      Python之元組的學(xué)習(xí)

      2、重復(fù)

      示例【2】

        allowUsers = ('root', 'westos', 'fentiao')  print allowUsers * 2  

      Python之元組的學(xué)習(xí)

      3、連接

      示例【3】

        allowUsers = ('root', 'westos', 'fentiao')  print allowUsers + ('fensi', 'fendai')

      Python之元組的學(xué)習(xí)

      4、成員操作符

      示例【4】

        allowUsers = ('root', 'westos', 'fentiao')  print 'westos' in allowUsers  print 'westos' not in allowUsers

      Python之元組的學(xué)習(xí)

      三、元組的應(yīng)用場景

      1、變量交換數(shù)值

      示例【1】:

        a = 1  b = 2  b, a = a, b    #實際上是先將(a,b)封裝成一個元組(1,2),然后在重新賦值  print a, b

      Python之元組的學(xué)習(xí)

      2、打印變量值

      示例【2】:

        name = 'westos'  age = 10  t = (name, age)    #將t定義成一個元組,也就是說最后打印的t,就是和(name,age)打印出來是一樣的  print 'name: %s,age:%d' % (name, age)  print 'name: %s,age:%d' % t

      Python之元組的學(xué)習(xí)

      3、元組的賦值

      示例【3】:

        t =('westos',10,100)  name,age,score =t    #將t的值按照索引分別賦給name,age,score  print name,age,score  scores = (100, 89, 45, 78, 53)  scores = sorted(scores)    #將scores按從小到大的順序排列  print scores

      Python之元組的學(xué)習(xí)

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號