元组

元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。

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 is 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 is 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

可以通过一对方括号来指明某个项目的位置从而来访问元组中的项目,就像我们对列表的用法一样。这被称作 索引 运算符。我们使用new_zoo[2]来访问new_zoo中的第三个项目。我们使用new_zoo[2][2]来访问new_zoo元组的第三个项目的第三个项目。

age = 22
name= 'swaroop'

print '%s is %d years old' % (name,age)
print 'why is %s playing with that python?' % name

print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制。

定制

字典:顾名思义 ,就是键 对应值。键应该是唯一的切不可变。 字典中的键 值,是没有顺序的。想要一个特定的顺序应该对它进行排序。

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


ab['guido']= 'guido@pyrhon.org'

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:
    print "
guido's address is %s" % ab['guido']

对字典的操作,定制使用。基本的了解完以后,准备开始爬数据没有。还没有想好。尽快吧

原文地址:https://www.cnblogs.com/sakura3/p/8378054.html