3.1 字典的定义

  • dictionary(字典) 是除列表以外Python之中最灵活的数据类型
  • 字典同样可以用来存储多个数据
    • 通常用于存储 描述一个 物体 的相关信息
  • 和列表的区别
    • 列表有序 的对象集合
    • 字典无序 的对象集合
  • 字典用{}定义
  • 字典使用键值对存储数据,键值对之间使用,分隔

    • key 是索引
    • value 是数据
    • 之间使用 : 分隔
    • 键必须是唯一的
    • 可以取任何数据类型,但 只能使用 字符串数字元组
      1. xiaoming = {"name": "小明",
      2. "age": 18,
      3. "gender": True,
      4. "height": 1.75}
      高级变量类型-字典-字符串 - 图1

      3.2 字典常用操作

  • 在 ipython3 中定义一个 字典,例如:xiaoming = {}

  • 输入 xiaoming. 按下 TAB 键,ipython 会提示 字典 能够使用的函数如下:

    1. In [1]: xiaoming.
    2. xiaoming.clear xiaoming.items xiaoming.setdefault
    3. xiaoming.copy xiaoming.keys xiaoming.update
    4. xiaoming.fromkeys xiaoming.pop xiaoming.values
    5. xiaoming.get xiaoming.popitem

    3.3 循环遍历

  • 遍历 就是 依次字典 中获取所有键值对

    1. # for 循环内部使用的 `key 的变量` in 字典
    2. for k in xiaoming:
    3. print("%s: %s" % (k, xiaoming[k]))

    提示:在实际开发中,由于字典中每一个键值对保存数据的类型是不同的,所以针对字典的循环遍历需求并不是很多

    3.4 应用场景

  • 尽管可以使用for in遍历字典

  • 但是在开发中,更多的应用场景是:
  • 使用 多个键值对,存储 描述一个 物体 的相关信息 —— 描述更复杂的数据信息
  • 多个字典 放在 一个列表 中,再进行遍历,在循环体内部针对每一个字典进行 相同的处理

    1. card_list = [{"name": "张三",
    2. "qq": "12345",
    3. "phone": "110"},
    4. {"name": "李四",
    5. "qq": "54321",
    6. "phone": "10086"}
    7. ]

    高级变量类型-字符串

    04. 字符串

    4.1 字符串的定义

  • 字符串就是一串字符,是编程语言中表示文本的数据类型

  • 在 Python 中可以使用一对双引号“或者一对单引号‘定义一个字符串
    • 虽然可以使用\”或者\’做字符串的转义,但是在实际开发中:
      • 如果字符串内部需要使用 “,可以使用 ‘ 定义字符串
      • 如果字符串内部需要使用 ‘,可以使用 “ 定义字符串
  • 可以使用索引获取一个字符串中指定位置的字符,索引计数从0开始
  • 也可以使用for循环遍历字符串中每一个字符

大多数编程语言都是用”来定义字符串

  1. string = "Hello Python"
  2. for c in string:
  3. print(c)

高级变量类型-字典-字符串 - 图2

4.2 字符串的常用操作

  • 在 ipython3 中定义一个 字符串,例如:hello_str = “”
  • 输入 hello_str. 按下 TAB 键,ipython 会提示 字符串 能够使用的 方法 如下:

    1. In [1]: hello_str.
    2. hello_str.capitalize hello_str.isidentifier hello_str.rindex
    3. hello_str.casefold hello_str.islower hello_str.rjust
    4. hello_str.center hello_str.isnumeric hello_str.rpartition
    5. hello_str.count hello_str.isprintable hello_str.rsplit
    6. hello_str.encode hello_str.isspace hello_str.rstrip
    7. hello_str.endswith hello_str.istitle hello_str.split
    8. hello_str.expandtabs hello_str.isupper hello_str.splitlines
    9. hello_str.find hello_str.join hello_str.startswith
    10. hello_str.format hello_str.ljust hello_str.strip
    11. hello_str.format_map hello_str.lower hello_str.swapcase
    12. hello_str.index hello_str.lstrip hello_str.title
    13. hello_str.isalnum hello_str.maketrans hello_str.translate
    14. hello_str.isalpha hello_str.partition hello_str.upper
    15. hello_str.isdecimal hello_str.replace hello_str.zfill
    16. hello_str.isdigit hello_str.rfind

    提示:正是因为 python 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求!

    1) 判断类型 - 9

    微信截图_20210725163250.png

    2) 查找和替换 - 7

    微信截图_20210725163342.png

    3) 大小写转换 - 5

    微信截图_20210725163444.png

    4) 文本对齐 - 3

    image.png

    5) 去除空白字符 - 3

    image.png

    6) 拆分和连接 - 5

    image.png

    4.3 字符串的切片

  • 切片方法适用于字符串列表元组

    • 切片 使用 索引值 来限定范围,从一个大的 字符串切出 小的 字符串
    • 列表元组 都是 有序 的集合,都能够 通过索引值 获取到对应的数据
    • 字典 是一个 无序 的集合,是使用 键值对 保存数据高级变量类型-字典-字符串 - 图9
    • 字符串[开始索引:结束索引:步长]

注意

  1. 指定的区间属于左闭右开型[开始索引, 结束索引)=>开始索引 >= 范围 < 结束索引
    • 从 起始 位开始,到 结束位的前一位 结束(不包含结束位本身)
  2. 从头开始,开始索引数字可以省略,冒号不能省略
  3. 到末尾结束,结束索引数字可以省略,冒号不能省略
  4. 步长默认为1,如果连续切片,数字和冒号都可以省略

    索引的顺序和倒序

  • 在 Python 中不仅支持顺序索引,同时还支持倒序索引
  • 所谓倒序索引就是从右向左计算索引
    • 最右边的索引值是 -1,依次递减

演练需求

  1. 截取从 2 ~ 5 位置 的字符串
  2. 截取从 2 ~ 末尾 的字符串
  3. 截取从 开始 ~ 5 位置 的字符串
  4. 截取完整的字符串
  5. 从开始位置,每隔一个字符截取字符串
  6. 从索引 1 开始,每隔一个取一个
  7. 截取从 2 ~ 末尾 - 1 的字符串
  8. 截取字符串末尾两个字符
  9. 字符串的逆序(面试题) ```python num_str = “0123456789”

1. 截取从 2 ~ 5 位置 的字符串

print(num_str[2:6])

2. 截取从 2 ~ 末尾 的字符串

print(num_str[2:])

3. 截取从 开始 ~ 5 位置 的字符串

print(num_str[:6])

4. 截取完整的字符串

print(num_str[:])

5. 从开始位置,每隔一个字符截取字符串

print(num_str[::2])

6. 从索引 1 开始,每隔一个取一个

print(num_str[1::2])

倒序切片

-1 表示倒数第一个字符

print(num_str[-1])

7. 截取从 2 ~ 末尾 - 1 的字符串

print(num_str[2:-1])

8. 截取字符串末尾两个字符

print(num_str[-2:])

9. 字符串的逆序(面试题)

print(num_str[::-1])

```