Python基础(二十)---字典

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key->value)对。
用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中

customers = {
    "name":"John Smith",
    "age":22,
    "is_verified":True
}
customers["name"]="Jon Smith"
customers["daliy"]="all time"
print(customers["name"])
print(customers.get("name"))
print(customers.get("birthday"))
print(customers.get("brithday","1999 11 24"))
print(customers["daliy"])

e.g.1
写一个程序,实现输入1234,输出one two three four

phone = input("Phone: ")
digits_mapping = {
    "1":"one",
    "2":"two",
    "3":"three",
    "4":"four"
}
output = ""
for ch in phone:
    output+=digits_mapping.get(ch,"!")+" "
print(output)

e.g.2
写一个程序,如果里边包含smile,将其转换为"(∩▽∩)",如果有sad,转换为"(╥╯^╰╥)",其他正常输出,以空格分隔

message = input(">")
words = message.split(" ")
emojis = {
	"smile":"(∩▽∩)",
	"sad":"(╥╯^╰╥)"
	}
output = ""
for word in words:
output += emojis.get(word,word)+" "
 print(output)

你可能感兴趣的:(Python3,python)