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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值