Exercise 39: Dictionaries, Oh Lovely Dictionaries

# create a mapping of state to abbreviation
states = {
‘Oregon‘: ‘OR‘,
‘Florida‘: ‘FL‘,
‘California‘: ‘CA‘,
‘New York‘: ‘NY‘,
‘Michigan‘: ‘MI‘ }
# create a basic set of states and some cities in them
cities = {
‘CA‘: ‘San Francisco‘,
‘MI‘: ‘Detroit‘,
‘FL‘: ‘Jacksonville‘ }
# add some more cities
cities[‘NY‘] = ‘New York‘
cities[‘OR‘] = ‘Portland‘
# print out some cities
print ‘-‘ * 10
print "NY State has: ", cities[‘NY‘]
print "OR State has: ", cities[‘OR‘]
# print some states
print ‘-‘ * 10
print "Michigan‘s abbreviation is: ", states[‘Michigan‘]
print "Florida‘s abbreviation is: ", states[‘Florida‘]
# do it by using the state then cities dict
print ‘-‘ * 10
print "Michigan has: ", cities[states[‘Michigan‘]]
print "Florida has: ", cities[states[‘Florida‘]]
# print every state abbreviat 

a = dict.get(‘A‘, ‘Default‘)

if ‘A‘ exists, print the value of ‘A‘; else print default value.

时间: 2024-10-22 09:58:09

Exercise 39: Dictionaries, Oh Lovely Dictionaries的相关文章

Exercise 39 - dictionary

# create a mapping of state to abbreviation states = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI' } # create a basic set of states and some cities in them cities = { 'CA': 'San Francisco', 'MI': 'Detroit',

Python Index

Table Of ContentsThe Hard Way Is EasierExercise 0: The SetupExercise 1: A Good First ProgramExercise 2: Comments And Pound CharactersExercise 3: Numbers And MathExercise 4: Variables And NamesExercise 5: More Variables And PrintingExercise 6: Strings

reviews of learn python3 the hard way

Almost every time,I try my best to write a long review of the book I have read. But this time I want to have a short one. learn python3 the hard way is a book which will teach you how to start to write codes. It has many exercises,through which you c

笨方法学python, Lesson 38, 39

Exercises 38 代码 ten_things = "Apples Oranges Crows Telephone Light Sugar" print "Wait there are not 10 things in that list. Let's fix that." stuff = ten_things.split(' ') more_stuff = ["Day", "Night", "Song&quo

PostgreSQL 全文检索

PostgreSQL 8.3.1  全文检索(转) 在postgreSQL 8.3自带支持全文检索功能,在之前的版本中需要安装配置tsearch2才能使用,安转配置tsearch2就不再多说了,主要介绍一下8.3中自带全文检索功能. 全文检索类型(Text Search Types) postgreSQL设计支持全文检索,提供两个数据类型(tsvector,tsquery),并且通过动态检索自然语言文档的集合,定位到最匹配的查询结果. tsvector 一个tsvector的值是唯一分词的分类列

pdf reference 格式详细说明

1. PDF概要 1.1. 图像模型 PDF能以平台无关.高效率的方式描叙复杂的文字.图形.排版. PDF 用图像模型来实现设备无关.图像模型允许应用程序以抽象对象描叙文字.图像.图标,而不是通过具体的像素点.把描述抽象对象的语言称作"页描述语言".PDF文档中存储都是以"页描述语言"表示的各种抽象对象. 要把PDF文档输出到具体设备上,产生具体像素点,需要有个翻译软件,把抽象对象渲染到具体输出设备上,称这个"翻译软件"为"规则扫描&q

Swift和C#的基本语法对比

Recently, Apple announced and released a beta version of the new Swift programming language for building iOS and OSX applications. Swift is a modern language with the power of Objective-C without the "baggage of C." While we can't argue that Obj

=-098765

http://ypk.39.net/search/all?k=%A1%B8%D4%E6%D1%F4%C3%D4%BB%C3%D2%A9%D4%F5%C3%B4%B9%BA%C2%F2Q%A3%BA%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7%A8%7C http://ypk.39.net/search/all?k=%A8%93%D2%CB%B3%C7%C3%D4%BB%C3%D2%A9%D4%F5%C3%B4%B9%BA%C2%F2

Looping and dictionaries

If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, print_hist prints each key and the corresponding value: Print dictionary in alphabetical order: Reverse lookup Given a dictionary d and a key k, it is e