• Python列表,元组,字典,序列,引用


    1.列表

    # Filename: using_list.py
    # This is my shopping list
    shoplist=["apple", "mango", "carrot", "banana"]
    print ("I have", len(shoplist), "items to purchase.")
    print ("These items are:"),
    for item in shoplist:
      print (item),
    print ("
    I also have to buy rice.")
    shoplist.append("rice")
    print ("My shopping list is now", shoplist)
    
    print ("I will sort my list now")
    shoplist.sort()
    print ("The first item I will buy is", shoplist[0])
    olditem=shoplist[0]
    del shoplist[0]
    print ("I bought the", olditem)
    print ("My shopping list is now", shoplist)
    

    2.元组

    # !/usr/bin/python
    # Filename: using_tuple.py
    
    zoo=("wolf", "elephant", "penguin")
    print ("number of animals in the zoo is", len(zoo))
    
    new_zoo=("monkey", "dolphin", zoo)
    print ("number of animals in the new zoo is", len(new_zoo))
    print ("all animals in new zoo are", new_zoo)
    print ("animals brought from old zoo are", new_zoo[2])
    print ("last animal brought from old zoo is", new_zoo[2][2])
    

    3.元组打印

    #!/usr/bin/python
    # Filename: print_tuple.py
    age = 22
    name = 'Swaroop'
    print ('%s is %d years old' % (name, age))
    print ('Why is %s playing with that python?' % name)
    

    4.字典

    #!/usr/bin/python
    # Filename: using_dict.py
    # 'ab' is short for 'a'ddress'b'ook
    ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
    'Larry' : 'larry@wall.org',
    'Matsumoto' : 'matz@ruby-lang.org',
    'Spammer' : 'spammer@hotmail.com'
    }
    print ("Swaroop's address is %s" % ab['Swaroop'])
    # Adding a key/value pair
    ab['Guido'] = 'guido@python.org'
    # Deleting a key/value pair
    del ab['Spammer']
    print ('
    There are %d contacts in the address-book
    ' % len(ab))
    for name, address in ab.items():
        print ('Contact %s at %s' % (name, address))
    if 'Guido' in ab: # OR ab.has_key('Guido')
        print ("
    Guido's address is %s" % ab['Guido'])
    

    5.序列

    #!/usr/bin/python
    # Filename: seq.py
    shoplist = ['apple', 'mango', 'carrot', 'banana']
    # Indexing or 'Subscription' operation
    print ('Item 0 is', shoplist[0])
    print ('Item 1 is', shoplist[1])
    print ('Item 2 is', shoplist[2])
    print ('Item 3 is', shoplist[3])
    print ('Item -1 is', shoplist[-1])
    print ('Item -2 is', shoplist[-2])
    # Slicing on a list
    print ('Item 1 to 3 is', shoplist[1:3])
    print ('Item 2 to end is', shoplist[2:])
    print ('Item 1 to -1 is', shoplist[1:-1])
    print ('Item start to end is', shoplist[:])
    # Slicing on a string
    name = 'swaroop'
    print ('characters 1 to 3 is', name[1:3])
    print ('characters 2 to end is', name[2:])
    

     6.引用

    #!/usr/bin/python
    # Filename: reference.py
    print ('Simple Assignment')
    shoplist = ['apple', 'mango', 'carrot', 'banana']
    mylist = shoplist # mylist is just another name pointing to the same object!
    del shoplist[0]
    print ('shoplist is', shoplist)
    print ('mylist is', mylist)
    # notice that both shoplist and mylist both print the same list without
    # the 'apple' confirming that they point to the same object
    print ('Copy by making a full slice')
    mylist = shoplist[:] # make a copy by doing a full slice
    del mylist[0] # remove first item
    print ('shoplist is', shoplist)
    print ('mylist is', mylist)
    # notice that now the two lists are different
    

     7.字符串

    #!/usr/bin/python
    # Filename: str_methods.py
    name = 'Swaroop' # This is a string object
    if name.startswith('Swa'):
        print ('Yes, the string starts with "Swa"')
    if 'a' in name:
        print ('Yes, it contains the string "a"')
    if name.find('war') != -1:
        print ('Yes, it contains the string "war"')
    delimiter = '_*_'
    mylist = ['Brazil', 'Russia', 'India', 'China']
    print (delimiter.join(mylist))
    
  • 相关阅读:
    mysql数据库优化课程---3、数据库设计是什么
    mysql数据库优化课程---2、命令其实也就是那几个单词
    mysql数据库优化课程---1、数据库的本质是什么
    php特级课---4、网站服务监控(常用网站服务监控软件有哪些)
    php特级课---5、网络数据转发原理
    php特级课---3、常用的网站加速技术有哪些
    php特级课---2、网站大数据如何存储
    php特级课---1、网站大访问量如何解决
    网络工程师课程---7、网络通信综合实验(做网络基础综合实验 用什么软件)
    Objective-C路成魔【2-Objective-C 规划】
  • 原文地址:https://www.cnblogs.com/oskb/p/5099708.html
Copyright © 2020-2023  润新知