python loops_[Python Basic] 什么是循环(1)

副标题:19. What are Loops (1)

这个专栏的文字, 不仅仅是学习 Python 的基础知识, 同时, 也按照 2/8 规律学习关键知识的关键部分 - python 核心英语词汇. 我们开始这一节的正文.

什么是 loops?

什么是 loops(循环)? 循环是一种控制结构,它回循环以重复一段代码。循环是要重复若干次的代码块的重复。与人类不同的是,电脑可以一次又一次地做重复性的工作而不会感到厌烦。英文:So, What are loops. Loops are control structures that loop back to repeat a block of codes. Loops are repetitions for a block of codes which you want to repeat for a number of times. Unlike human beings, computers can do repetitive tasks over and over again without getting bored.

print(1)

print(2)

print(3)

print(4)

print(5)

print(6)

print(7)

print(8)

print(9)

print(10)

例如,我们有这样如下一段代码,它会重复10次,基本上是一遍又一遍重复执行相同的任务,使它成为一个更优雅的代码,而不是在每一行上都编写print()函数,我们可以使用while循环。英文: For example, we have this piece of code that repeats itself 10 times, basically doing the very same tasks repeatedly again and again, To make it a more elegant code, instead of coding each print() function on each and every line, we can use a while loop.

loops 举例:

代码编写原则: 少即是多

在编写代码时,请始终提醒自己。 代码越少,出错的地方就越少, 更少出错的地方, 意味着更少的调试代码, 更少的调试代码意味着更少的代码需要去读, 更少的代码读取意味着, 你可以更清楚地看到整个框架。从本质上讲,少即是多。英文: Always remind ourselves when coding that less code means less places to make mistakes, and less places to make mistakes means less code to debug, and less code to debug means less code to read,and less code to read means that you can see the whole picture so much clearer.

In essence, less is more.

count = 1

while count <= 10:

print(count)

count = count + 1

# output:

1

2

3

4

5

6

7

8

9

10

现在,只要 while 条件为 True,将执行缩进的代码块。并且循环将继续其迭代。缩进的代码块打印出计数。 然后将计数递增1。然后,它循环回来再次检查While条件。一旦条件变为 False,循环就结束。英文: Now, as long as the while condition is True,the indented block of codes will be executed and the loop will continue its iteration. The indented block of codes prints out the count and then increment the count by 1. Then it loops back to check for the while condition again. Once the condition becomes False, the loop ends.As mentioned in the video on Conditionals, the line preceding any indented block of codes must end with a colon. 机译: 正如在条件语句视频中提到的,任何缩进代码块之前的行, 都必须以冒号结束。

# print(count) 和 count = count + 1 对换一下位置:

>>> count = 1

>>> while count <= 10:

... count = count + 1

... print(count)

...

2

3

4

5

6

7

8

9

10

11

## output: 得到不一样的结果: 从 2 - 10. 而不是从 1 - 10.

## 原因是顺序执行程序, 第一个 print 输出就是 2.

如何 break 一个循环

现在,如果您想在WHILE条件变为假之前退出WHILE循环,会发生什么呢? 您可以在缩进的代码块中使用Break关键字。英文:Now, what happens when you want to break out of the while loop before the while condition becomes False ? You can use the break keyword within the indented block of codes.

count = 1

while count <= 10:

print(count)

count = count + 1

to_exit = input("Enter e if you wish to exit ") if to_exit == "le":

break

在这里,我们使用了Python内置函数input(), 用来提示用户输入 e (如果他想提前退出 while 循环), 如果用户在提示时键入e 并按Enter键,While 循环将中断并结束。否则它将继续循环。 这个中断操作将打破 While 循环。英文: Here, we used the Python built-in function input() to prompt the user to enter the e if he wishes to exit from the while loop prematurely, If the user types an e and press the Enter key when prompted, the while loop will break and end, otherwise it will continue the loop.The break will break out of the while loop.

input() 函数只录入第一个任意字符的情况

input()函数还可以强制自己只捕获用户输入的第一个字符,而忽略其他字符。 英文:The input() function can also force itself to capture only the first character that the user input, and ignore the rest.

to_exit = input("Enter e if you wish to exit") # 编号 1

to_exit = input("Enter e if you wish to exit ") [0] # 编号 2

你要做的, 仅仅是把 to_exit = input("Enter e if you wish to exit")

换成 to_exit = input("Enter e if you wish to exit") [0]

它会将变量to_exit 捕获为, 用户输入的任何内容的第一号索引 (index 0)英文:

What you need to do is just replace "to_exit = input("Enter e if you wish to exit")"

with "to_exit = input("Enter e if you wish to exit") [0]"

which will capture the variable to_exit as index 0 of whatever was input by the user.

对于第一个输入示例,如果用户输入ee而不是e,那么它将不会跳出while循环。只有输入e 才会打破while循环。 英文: For the first input example, if the user types ee instead of e, it will NOT break out of the while loop. Only typing e will break the while loop.

对于第二个输入示例,如果用户类型ее代替e,它将打破while循环。 英文: For the second input example, if the user types ее instead of e, it WILL break out of the while loop.

如何跳入下一个循环 --关键字 continue

只要输入的第一个字符是 e 字符,如果你仅仅想跳到下一个迭代,而不是跳出While循环,那该怎么办呢?英文:As long as the first charater of whatever is input, is the e character, What if you only want to skip to the next iteration, instead of breaking out of the while loop.

count = 0

while count <= 9:

count = count + 1

if count%2:

continue

print(count)

#output : 10

此处,当count%2 得出的余数为非 0 时,if 条件返回 True,因此由于 continue 关键字,它不会 print(count), 它立即跳到 while 循环的下一个迭代。英文: Here, when count divided by 2 gives a non-zero remainder, the if condition returns True, so it will not print count, because of the continue keyword, which immediately skips to the next iteration of the while loop.

因此,只有当 count%2 给出一个余数 0,它有效地返回 False, 导致打印输出 1 到 10 之间的所有偶数。英文:Therefore, effectively, it will only print the count, when count divided by 2 gives a zero remaninder, which effectively returns False, resulting in printing all the even numbers between 1 to 10.最后, 请注意,在 While 中使用了 Break 和 Continue,以及 For 循环. 英文: Note that, Break and Continue are used inside While, as well as, For Loops.

词汇:

iteration 迭代

概念:

the loop will continue its iteration

发布时间: 2020 年 2 月 28 日

知乎链接:[Python Basic] 什么是循环(1)

当前可任意转载,转载请保存以上信息即可。无需获得授权.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值