标签:style blog color 使用 ar 数据 sp div on
上周总结了一下Python的一些基本数据类型和用法。这次总结一下4-9章的内容,完后,赶紧学以致用吧。
字典是Python中唯一内建的映射类型。字典中的值并没有特殊的顺序,但是都存储在一个特定的键(Key)里。键可以是数字、字符串甚至是元组。其实用过Json的同学就会觉得Python的字典很亲切。
1 >>> phonebook = {‘Alice‘: ‘2341‘, ‘Beth‘: ‘9102‘, ‘Cecil‘: ‘3258‘}
1 >>> items = [(‘name‘, ‘Gumby‘),(‘age‘, 42)] 2 >>> phonebook = dict(items) 3 >>> phonebook 4 {‘age‘: 42, ‘name‘: ‘Gumby‘}
1 >>> items = dict(name=‘Gumby‘, age=42) 2 >>> items 3 {‘age‘: 42, ‘name‘: ‘Gumby‘}
1 >>> {}.fromkeys([‘name‘,‘age‘]) 2 {‘age‘: None, ‘name‘: None} 3 >>> dict.fromkeys([‘name‘,‘age‘]) 4 {‘age‘: None, ‘name‘: None} 5 >>> dict.fromkeys([‘name‘,‘age‘], ‘unknown‘) 6 {‘age‘: ‘unknown‘, ‘name‘: ‘unknown‘}
1 >>> items 2 {‘age‘: 42, ‘name‘: ‘Gumby‘} 3 >>> "%(name)s‘s age is %(age)s." % items 4 "Gumby‘s age is 42."
1 >>> template = ‘‘‘<html> 2 ... <head><title>%(title)s</title></head> 3 ... <body> 4 ... <h1>%(title)s</h1> 5 ... <p>%(text)s</p> 6 ... </body>‘‘‘ 7 >>> data = {‘title‘: ‘Panderen Home‘, ‘text‘: ‘Welcome to my home page‘} 8 >>> print template % data 9 <html> 10 <head><title>Panderen Home</title></head> 11 <body> 12 <h1>Panderen Home</h1> 13 <p>Welcome to my home page</p> 14 </body>
标签:style blog color 使用 ar 数据 sp div on
原文地址:http://www.cnblogs.com/panderen/p/3997016.html