zoukankan      html  css  js  c++  java
  • Python体验(03)列表list和元组tuple

    示例1:LIST

    取得list长度len(list),向list追加元素list.append(),list排序list.sort(),删除list元素 del list[index]

    代码
    #!/usr/bin/python
    #
    filename:list.py

    #This is my shopping list
    shoplist = ['apple','mango','carrot','banana']
    print '\nI have',len(shoplist),'items to purchase:'
    for item in shoplist:
            
    print item

    print '\nAdd rice to the store: append()'
    shoplist.append(
    'rice')
    print '\t ',shoplist

    print '\nSORT the store: sort()\t'
    shoplist.sort()
    print '\t',shoplist

    print '\nDEL item 0, the resule: del'
    del shoplist[0]
    print '\t',shoplist,'\n\n'


     示例2:tuple(元组)

    列表中的列表不会丢失它的身份;可以认为列表中可存储列表,整个列表作为一个元素对待;可以索引到列表后再继续索引列表内的数据;注意最后通配符的用法: print '%s is %d years old' % (name, age)
    print 'Why is %s playing with that python?' % name

    代码
    #!/usr/bin/python
    #filename:tuple.py

    zoo = ['wolf','elephant','penguin']
    new_zoo = ('monkey','dolphin',zoo)

    print 'The OLD zoo had',len(zoo),'annimals:'
    print '\t',zoo

    print '\nThe NEW zoo have',len(new_zoo),'annimals:'
    print '\t',new_zoo

    print'\nAnimals brought from OLD zone are:',new_zoo[2#zoo,the index is 2
    print'Last animal brought from OLD zoo is: %s\n'%new_zoo[2][2]#zoo[2]

    #--==------------ the output is  -----------------==--
    #phoenix@debian:~/py$ python tuple.py
    #The OLD zoo had 3 annimals:
    #        ['wolf', 'elephant', 'penguin']

    #The NEW zoo have 3 annimals:
    #        ('monkey', 'dolphin', ['wolf', 'elephant', 'penguin'])

    #Animals brought from OLD zone are: ['wolf', 'elephant', 'penguin']
    #Last animal brought from OLD zoo is: penguin


  • 相关阅读:
    net.sf.json.JSONObject maven下载到了但是java后台一直用不了问题
    创建springboot2.1项目运行报错
    百度地图,加载顺序异步问题,用定时器解决
    大话设计模式--(1)简单工厂模式
    H5页面单点登录跳回首页 http url参数转义
    H5页面,百度地图点击事件
    批量给数据两边加上双引号和逗号
    java基础源码 (6)--ArrayListt类
    前端 移动端H5页面 DEBUG
    H5页面,华为手机打开不加载JS的问题
  • 原文地址:https://www.cnblogs.com/flaaash/p/1882929.html
Copyright © 2011-2022 走看看