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‘,
    ‘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 abbreviation
print(‘-‘ * 10)
for state, abbrev in states.items():
    print("%s is abbreviated %s" % (state, abbrev))

# print every city in state
print(‘-‘ * 10)
for abbrev, city in cities.items():
    print("%s has the city %s" % (abbrev, city))

# do both at the same time
print(‘-‘ * 10)
for state, abbrev in states.items():
    print("%s state is abbreviated %s and had city %s" % (
              state, abbrev, cities[abbrev]))

print(‘-‘ * 10)
# safely get an abbreviation by state that might not be there
state = states.get(‘Texas‘, None)

if not state:
    print("Sorry, no Texas")

# get a city with a default values
city = cities.get(‘TX‘, ‘Does Not Exist‘)
print("The city for the state ‘TX‘ is: %s" % city)

output

----------
NY State has:  New York
OR State has:  Portland
----------
Michigan‘s abbreviation is:  MI
Florida‘s abbreviation is:  FL
----------
Michigan has:  Detroit
Florida has:  Jacksonville
----------
Oregon is abbreviated OR
Florida is abbreviated FL
California is abbreviated CA
New York is abbreviated NY
Michigan is abbreviated MI
----------
CA has the city San Francisco
MI has the city Detroit
FL has the city Jacksonville
NY has the city New York
OR has the city Portland
----------
Oregon state is abbreviated OR and had city Portland
Florida state is abbreviated FL and had city Jacksonville
California state is abbreviated CA and had city San Francisco
New York state is abbreviated NY and had city New York
Michigan state is abbreviated MI and had city Detroit
----------
Sorry, no Texas
The city for the state ‘TX‘ is: Does Not Exist

2019-10-01

02:05:37

原文地址:https://www.cnblogs.com/petitherisson/p/11614165.html

时间: 2024-08-05 04:07:52

Exercise 39 - dictionary的相关文章

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

笨方法学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

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

mysql安装一 rpm包安装

centos65上安装mysql一 梳理了一下centos上安装mysql的三种方法:一是通过源码安装,二是用通用二进制包,三是通过不同linux release到系统包安装,centos上是RPM包安装. 先来rpm包装 0 环境 [[email protected] ~]# uname -a Linux host2 2.6.32-504.3.3.el6.x86_64 #1 SMP Wed Dec 17 01:55:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Li

键盘钩子

发段代码纯属娱乐. 本人郑重声明:若被用于其他非法用途,本人不承担法律责任. 捕获键盘输入,区分大小写,区分上下档输入. 我电脑的某些键ascII码值与ascII表中的值不一致,所以重新做了个map表.若你的一致,只须稍微改下代码即可. 键盘输入保存在d盘根目录下.杀毒软件可能会拦截,允许操作就可以了. #define _WIN32_WINNT 0x0400 #define STRICT #define WIN32_LEAN_AND_MEAN #define WH_KEYBOARD_LL 13

枚举帮助类

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Linq; 5 6 namespace EnumHelper 7 { 8 /// <summary> 9 /// 枚举帮助类 10 /// 1.获取枚举的描述文本 11 /// 2.获取枚举名和描述信息的列表 12 /// </summary> 13 public static class

(二十七)c#Winform自定义控件-多输入窗体

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  准备工作 这个窗体继承子基类窗体FrmWithOKCancel1,如果你对FrmWithOKCancel1还不了解,请移步 (二十五)c#Winform自定义控件-有确定取消的窗体(一) 查看 开始 添

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