Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
70
6
while
循环和
for
循环
管他们这一套那一套,什么低贱的劳动那一套……
——
Robert Burns
For a’ That and a’ That
使用
if
elif
else
测试的时候是自上而下进行的。但是有时候需要重复某些操作。我
们要的是
循环
,而
Python
提供了两种选择:
while
for
6.1
while
循环
Python
中最简单的循环机制是
while
。使用交互式解释器尝试下面这个例子,这个简单的
循环会打印出从
1
5
的数字:
>>> count = 1
>>> while count <= 5:
... print(count)
... count += 1
...
1
2
3
4
5
>>>
首先将变量
count
赋值为
1
while
循环会比较
count
的值和
5
的大小关系,如果
count
于或等于
5
,就继续执行。在循环内部,打印
count
的值,然后使用语句
count += 1
其值
增加
1
Python
返回到循环的开始位置
,继续比较
count
5
count
的值现在为
2
while
循环因此会再次执行,
count
的值增加为
3
while
循环和
for
循环
71
count
的值从
5
增加到
6
之前循环一直进行。在下次循环时,条件
count <= 5
为假,
while
循环结束。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223