Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21016
  • 博文数量: 27
  • 博客积分: 585
  • 博客等级: 中士
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-20 17:11
文章分类

全部博文(27)

文章存档

2013年(1)

2012年(26)

我的朋友
最近访客

分类: Python/Ruby

2012-12-27 10:35:52

十一、元组的使用

元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元组。元组通过圆

 

括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值

 

的时候,即被使用的元组的值不会改变

 

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]

 

 

----------------------------------------

 

Number of animals in the zoo is 3

Number of animals in the new zoo is 3

All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))

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

Last animal brought from old zoo is penguin

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 '\nThere are %d contacts in the address-book\n' % 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 "\nGuido's address is %s" % ab['Guido']

 

十二、列表的使用

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]   #13表示的是第二个数也就是shoplist[1],shoplist[2]第三个数

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:]

print 'characters 1 to -1 is', name[1:-1]

print 'characters start to end is', name[:]

 

----------------------------------------------------------

Item 0 is apple

Item 1 is mango

Item 2 is carrot

Item 3 is banana

Item -1 is banana

Item -2 is carrot

Item 1 to 3 is ['mango', 'carrot']

Item 2 to end is ['carrot', 'banana']

Item 1 to -1 is ['mango', 'carrot']

Item start to end is ['apple', 'mango', 'carrot', 'banana']

characters 1 to 3 is wa

characters 2 to end is aroop

characters 1 to -1 is waroo

characters start to end is swaroop

 

 

阅读(280) | 评论(0) | 转发(0) |
0

上一篇:python--part2

下一篇:python--part4

给主人留下些什么吧!~~