Python3快速入门知识点:流程控制、迭代器、生成器

2022-10-2012:58:12后端程序开发Comments763 views字数 2119阅读模式

流程控制

if 控制

if 表达式1:    语句    if 表达式2:        语句    elif 表达式3:        语句    else:        语句elif 表达式4:    语句else:    语句

1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。3、在 Python 中没有 switch - case 语句。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

三元运算符:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

<表达式1> if <条件> else <表达式2>

编写条件语句时,应该尽量避免使用嵌套语句。嵌套语句不便于阅读,而且可能会忽略一些可能性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

for 遍历

for <循环变量> in <循环对象>:    <语句1>else:    <语句2>

else 语句中的语句2只有循环正常退出(遍历完所有遍历对象中的值)时执行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

在字典中遍历时,关键字和对应的值可以使用 items() 方法同时解读出来:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

knights = {gallahad: the pure, robin: the brave}for k, v in knights.items():    print(k, v)

在序列中遍历时,索引位置和对应值可以使用 enumerate() 函数同时得到:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

for i, v in enumerate([tic, tac, toe]):    print(i, v)

同时遍历两个或更多的序列,可以使用 zip() 组合:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

questions = [name, quest, favorite color]answers = [lancelot, the holy grail, blue]for q, a in zip(questions, answers):    print(What is your {0}?  It is {1}..format(q, a))

要反向遍历一个序列,首先指定这个序列,然后调用 reversed() 函数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

for i in reversed(range(1, 10, 2)):    print(i)

要按顺序遍历一个序列,使用 sorted() 函数返回一个已排序的序列,并不修改原值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

basket = [apple, orange, apple, pear, orange, banana]for f in sorted(set(basket)):    print(f)

while 循环

while<条件>:    <语句1>else:    <语句2>

break、continue、pass

break 语句用在 while 和 for 循环中,break 语句用来终止循环语句,即循环条件没有 False 条件或者序列还没被完全递归完,也会停止执行循环语句。  continue 语句用在 while 和 for 循环中,continue 语句用来告诉 Python 跳过当前循环的剩余语句,然后继续进行下一轮循环。continue 语句跳出本次循环,而 break 跳出整个循环。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

pass 是空语句,是为了保持程序结构的完整性。pass 不做任何事情,一般用做占位语句。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

迭代器

  • 迭代器是一个可以记住遍历的位置的对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 迭代器有两个基本的方法:iter() 和 next()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 字符串,列表或元组对象都可用于创建迭代器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

迭代器可以被 for 循环进行遍历:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

li = [1, 2, 3]it = iter(li)for val in it:    print(val)

迭代器也可以用 next() 函数访问下一个元素值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

import sysli = [1,2,3,4]it = iter(li)while True:    try:        print (next(it))    except StopIteration:        sys.exit()

生成器

  • 在 Python 中,使用了 yield 的函数被称为生成器(generator)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回 yield 的值, 并在下一次执行 next() 方法时从当前位置继续运行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

  • 调用一个生成器函数,返回的是一个迭代器对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

import sysdef fibonacci(n): # 生成器函数 - 斐波那契    a, b, counter = 0, 1, 0    while True:        if (counter > n):             return        yield a        a, b = b, a + b        counter += 1f = fibonacci(10) # f 是一个迭代器,由生成器返回生成while True:    try:        print(next(f))    except StopIteration:        sys.exit()

end...未完待续文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28733.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/28733.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定