## Python教程-循环语句 顶原

智能小松鼠

while

1、http://www.yiibai.com/python/python_loops.html

2、http://www.yiibai.com/python/python_while_loop.html

3、http://www.cnblogs.com/way_testlife/archive/2010/06/14/1758276.html

4、http://www.runoob.com/python/python-while-loop.html

``````while expression:
statement(s)``````
``````#!/usr/bin/python

count = 0
while (count < 9):
print 'The count is:', count
count = count + 1

print "Good bye!"``````
``````The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!``````

## The Infinite Loop:

``````#!/usr/bin/python

var = 1
while var == 1 :  # This constructs an infinite loop
num = raw_input("Enter a number  :")
print "You entered: ", num

print "Good bye!"``````
```Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
File "test.py", line 5, in <module>
num = raw_input("Enter a number :")
KeyboardInterrupt```

## The else Statement Used with Loops

``````#!/usr/bin/python

count = 0
while count < 5:
print count, " is  less than 5"
count = count + 1
else:
print count, " is not less than 5"``````
```0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5```

## Single Statement Suites:

``````#!/usr/bin/python

flag = 1

while (flag): print 'Given flag is really true!'

print "Good bye!"``````

for

1、http://www.yiibai.com/python/python_for_loop.html

``````for iterating_var in sequence:
statements(s)``````
``````#!/usr/bin/python

for letter in 'Python':     # First Example
print 'Current Letter :', letter

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # Second Example
print 'Current fruit :', fruit

print "Good bye!"``````
```Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!```

1、http://www.yiibai.com/python/python_nested_loops.html

```for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)```
``````while expression:
while expression:
statement(s)
statement(s)``````
``````#!/usr/bin/python

i = 2
while(i < 100):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print i, " is prime"
i = i + 1

print "Good bye!"``````
``````2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Good bye!``````

## 循环控制语句：

break

``````#!/usr/bin/python

for letter in 'Python':     # First Example
if letter == 'h':
break
print 'Current Letter :', letter

var = 10                    # Second Example
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break

print "Good bye!"``````
```Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!```

pass

``````#!/usr/bin/python

for letter in 'Python':
if letter == 'h':
pass
print 'This is pass block'
print 'Current Letter :', letter

print "Good bye!"``````
```Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!```

continue

``````#!/usr/bin/python

for letter in 'Python':     # First Example
if letter == 'h':
continue
print 'Current Letter :', letter

var = 10                    # Second Example
while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print "Good bye!"``````
```Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!```

### 智能小松鼠

QA/测试工程师

2018/05/10
0
0

2018/05/10
0
0

“死循环”是必须要避免的，当然“活循环”也未必都要执行完。 break 语句 比如我们要从一个列表中找到某个特定元素，那么只要找到了这次循环就可以停止了，没有必要非得走到最后。 这种情况...

2018/05/10
0
0
Python编程入门——基础语法详解

2018/08/19
0
0
python超详细的基础笔记你学会了么

python简介 python是一种面向对象的解释型计算机程序设计语言，python的是吉多·范罗苏姆（Guido van Rossum）于1989年发明 任何语言都有优缺点，python也不例外，python的优点是简单易懂、可...

2018/08/15
0
0

mysql mysql中的索引

edison_kwok
8分钟前
0
0
Springboot 集成redis

13分钟前
1
0
Qt编写安防视频监控系统4-删除视频

17分钟前
0
0
NCRE考试感想 三级信息安全(下)

19分钟前
0
0
MySQL索引原理及慢查询优化，了解一下？

MySQL凭借着出色的性能、低廉的成本、丰富的资源，已经成为绝大多数互联网公司的首选关系型数据库。 虽然性能出色，但所谓“好马配好鞍”，如何能够更好的使用它，已经成为开发工程师的必修课...

24分钟前
0
0