学习《简明Python教程》第六天

 第九章 数据结构

Python中内建三种数据结构:

    列表 方括号中逗号隔开的项目,列表是可变的数据类型

    元组 圆括号中逗号隔开的项目,元组的值不会改变

    字典

 

  
  
  
  
  1. #!/usr/bin/python 
  2. #Filename using_list.py 
  3.  
  4. shoplist = ['apple''mango''carrot''banana'
  5.  
  6. print 'I have', len(shoplist), 'items to purchase.' 
  7.  
  8. print 'These items are:'
  9. for item in shoplist: 
  10.     print item, 
  11.  
  12. print '\nI also have to buy rice.' 
  13. shoplist.append('rice'
  14. print 'My shopping list is now', shoplist 
  15.  
  16. print 'I will sort my list now' 
  17. shoplist.sort() 
  18. print 'Sorted shopping list is', shoplist 
  19.  
  20. print 'The first item I will buy is', shoplist[0
  21. olditem = shoplist[0
  22. del shoplist[0
  23. print 'I bought the', olditem 
  24. print 'My shopping list is now',shoplist 

$python using_list.py

I have 4 items to purchase.

These items are: apple mango carrot banana

I also have to buy rice.

My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']

I will sort my list now

Sorted shopping list is ['apple', 'mango', 'carrot', 'banana', 'rice']

The first item I will buy is apple

I bought the apple

My shopping list is now ['mango', 'carrot', 'banana', 'rice']

 

###print 语句结尾使用一个逗号来消除换行符

  
  
  
  
  1. #!/usr/bin/python 
  2. #Filename using_tuple.py 
  3.  
  4. zoo = ('wolf''elephant''penguin'
  5. print 'Number of animals in the zoo is', len(zoo) 
  6.  
  7. new_zoo = ('mokey''dolphin', zoo) 
  8. print 'Number of animals in the new zoo is', len(new_zoo) 
  9. print 'All animals in new zoo are', new_zoo 
  10. print 'Animals brought from old zoo are', new_zoo[2
  11. print 'Last animal brought from old zoo is', new_zoo[2][2

$python using_tule.py

Number of animals in the zoo is 3

Number of animals in the new zoo is 3

All animals is new zoo are ('mokey', 'dolphin', ('wolf', 'elephant', 'penguin'))

Animals brought from old zoo are ('wolf', 'elephant', 'penguin')

Last animal brought from old zoo is penguin

 

###含有0个或1个项目的元组:

   myempty = ()

   singleton = (2, ) #必须在单一的项目后跟一个逗号,区分元组和表达式中一个带圆括号的对象

 

  
  
  
  
  1. #!/usr/bin/python 
  2. #Filename print_tuple.py 
  3.  
  4. age = 22 
  5. name = 'Swaroop' 
  6.  
  7. print '%s is %d years old' % (name, age) 
  8. print 'Why is %s playing with that python?' % name 

$python print_tuple.py

Swaroop is 22 years old

Why is Swaroop playing with that python?

 

  
  
  
  
  1. #!/usr/bin/python 
  2. #Filename using_dict.py 
  3.  
  4. #'ab' is short for 'a'ddress'b'ook 
  5.  
  6. ab = {   'Swaroop'   : '[email protected]'
  7.          'Larry'     : '[email protected]'
  8.          'Matsumoto' : '[email protected]'
  9.          'Spammer'   : '[email protected]' 
  10.       } 
  11. print "Swarrop's address is %s" % ab['Swaroop'] 
  12.  
  13. #Adding a key/value pair 
  14. ab['Guido'] = '[email protected]' 
  15.  
  16. #Deleting a key/value pair 
  17. del ab['Spammer'
  18.  
  19. print '\nThere are %d contacts in the address-book\n' % len(ab) 
  20. for name, address in ab.items(): 
  21.     print 'Contact %s at %s' % (name, address) 
  22.  
  23. if 'Guido' in ab: # OR ab.has_key('Guido') 
  24.     print "\nGuido's address is %s" % ab['Guido'] 

你可能感兴趣的:(python)