Python:字典(Dictionary)基础应用

 字典Dictionary 键值对的形式,一个key对应一个value,一个dictionary中,key是唯一的
get()可以在访问到不存在的量时,可以人为设置值
而且 get()即使访问不到,程序也会正常运行
但是print(customer["birthday"]) 会直接结束程序

customer = {
    "name": "Stella",
    "age": 18,
    "is_verified": True
}
# 修改
customer["name"] = "jack "
# 增加
customer["telephone"] = 123456
print(customer["telephone"])
# 访问方式
print(customer["name"])
print(customer.get("name"))
# print(customer["birthday"])
print(customer.get("birthday"))
print(customer.get("birthday", "Gracia 1 19999"))

test:提示输入Phone  (输入一串数字),输出英文形式

例如  Phone 1234

输出 one two three four

tips:"1"->"one"


答案:

telephone = {
    "1": "one",
    "2": "two",
    "3": "three",
    "4": "four",
    "5": "five"
}
output = ""
number = input("Phone ")
for x in number:
    # output+=telephone[x]+" "
    # 用get()保证当没有这个值时不会终止程序,且有标识
    output += telephone.get(x, "!") + " "
print(output)

相关推荐

  1. Python:字典(Dictionary)基础应用

    2024-07-23 10:14:02       20 阅读
  2. 【大模型应用开发-python基础】(九)python字典

    2024-07-23 10:14:02       34 阅读
  3. Python字典基本用法

    2024-07-23 10:14:02       22 阅读
  4. Python 字典(Dict)详解与实战应用

    2024-07-23 10:14:02       15 阅读
  5. Python基础dict字典定义与函数

    2024-07-23 10:14:02       53 阅读
  6. python基础教程六(字典方法)

    2024-07-23 10:14:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 10:14:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 10:14:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 10:14:02       45 阅读
  4. Python语言-面向对象

    2024-07-23 10:14:02       55 阅读

热门阅读

  1. 数据结构C++——矩阵【详】

    2024-07-23 10:14:02       15 阅读
  2. 问百度文心一言 下三角矩阵

    2024-07-23 10:14:02       16 阅读
  3. cookie和session的区别

    2024-07-23 10:14:02       17 阅读
  4. 图像预处理(基础功能)

    2024-07-23 10:14:02       18 阅读
  5. 014集——RSA非对称加密——vba源代码

    2024-07-23 10:14:02       21 阅读
  6. 如何面对压力和动力

    2024-07-23 10:14:02       21 阅读
  7. iphone11 如何打开开发者模式?

    2024-07-23 10:14:02       18 阅读
  8. centos7 yum更换国内源【超简洁步骤】

    2024-07-23 10:14:02       15 阅读
  9. Kotlin 继承

    2024-07-23 10:14:02       11 阅读
  10. LeetCode718. 最长重复子数组

    2024-07-23 10:14:02       13 阅读
  11. MySQL的查询优化思路

    2024-07-23 10:14:02       15 阅读