python的公共方法

1、 python包含以下内置函数:

注意:字符串 比较 符合以下规则:‘0’<'A'<'a'。

2、切片 

      

  •  切片 使用索引值来限定范围,从一个大的字符串中切出小的字符串
  • 列表 元组 都是 有序 的集合,都能够 通过索引值 获取到对应的数据
  • 字典 是一个 无序 的集合,是使用 键值对 保存数据

3、算数运算符及对比列表追加方法

(1)、列表追加方法

(2)算术运算符

  • in在对 字典 操作时,判断的是 字典的键
  • in和not in被称为 成员运算符

5、完整的 for循环语法如下:

列1:

for num in [1, 2, 3]:
    print(num)
    if num == 2:
        break
else:
    # 如果循环体内部使用break退出了循环
    # else 下方的代码就不会被执行
    print("会执行吗?")
print("循环结束")

列2:

students = [
    {"name": "阿土"},
    {"name": "小美"}
]
# 在学员列表中搜索指定的姓名
find_name = "张三"
for stu_dict in students:
    print(stu_dict)
    if stu_dict["name"] == find_name:
        print("找到了 %s" % find_name)
        # 如果已经找到,应该直接退出循环,而不再遍历后续的元素
        break
    # else:
    #     print("抱歉没有找到 %s" % find_name)
else:
    # 如果希望在搜索列表时,所有的字典检查之后,都没有发现需要搜索的目标
    # 还希望得到一个统一的提示!
    print("抱歉没有找到 %s" % find_name)
print("循环结束")

猜你喜欢

转载自blog.csdn.net/qq_41842628/article/details/84635358
今日推荐