Contents

控制流

顺序结构

```   1 import math
3 area = math.pi * (radius ** 2)
4 print area
```

分支结构

```   1 #!/usr/bin/python
2
3 number = 23
4 guess = int(raw_input('Enter an integer : '))
5
6 if guess == number:
7     print 'Congratulations, you guessed it.' # New block starts here
8     print "(but you do not win any prizes!)" # New block ends here
9 elif guess < number:
10     print 'No, it is a little higher than that' # Another block
11     # You can do whatever you want in a block ...
12 else:
13     print 'No, it is a little lower than that'
14     # you must have guess > number to reach here
15
16 print 'Done'
17 # This last statement is always executed, after the if statement is executed
```

```Enter an integer : 50
No, it is a little lower than that
Done```

```Enter an integer : 22
No, it is a little higher than that
Done```

```Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done ```

elif和else从句都必须在逻辑行结尾处有一个冒号，下面跟着一个相应的语句块（当然还包括正确的缩进）。

```   1 if True:
2     print 'Yes, it is true'
```

```给C/C++程序员的注释

```>>> flag = True
>>> if flag: print 'Yes'
...
Yes```

循环结构

```   1 #!/usr/bin/python
2
3 number = 23
4 running = True
5
6 while running:
7     guess = int(raw_input('Enter an integer : '))
8
9     if guess == number:
10         print 'Congratulations, you guessed it.'
11         running = False # this causes the while loop to stop
12     elif guess < number:
13         print 'No, it is a little higher than that'
14     else:
15         print 'No, it is a little lower than that'
16 else:
17     print 'The while loop is over.'
18     # Do anything else you want to do here
19
20 print 'Done'
```

```Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done```

True和False被称为布尔类型。你可以分别把它们等效地理解为值1和0。在检验重要条件的时候，布尔类型十分重要，它们并不是真实的值1。

```给C/C++程序员的注释

for..in是另外一种循环语句，它在一序列的对象上 递归 即逐一使用队列中的每个项目。比如

```   1 #!/usr/bin/python
2
3 for i in range(1, 5):
4     print i
5 else:
6     print 'The for loop is over'
```

```1
2
3
4
The for loop is over```

for循环在这个范围内递归——for i in range(1,5)等价于for i in [1, 2, 3, 4]，这就如同把序列中的每个数（或对象）赋值给i，一次一个，然后以每个i的值执行这个程序块。在这个例子中，我们只是打印i的值。

```给C/C++/Java/C#程序员的注释
Python的for循环从根本上不同于C/C++的for循环。C#程序员会注意到Python的for循环与C#中的foreach循环十分类似。Java程序员会注意到它与Java 1.5中的for (int i : IntArray)相似。

```   1 #!/usr/bin/python
2
3 while True:
4     s = raw_input('Enter something : ')
5     if s == 'quit':
6         break
7     print 'Length of the string is', len(s)
8 print 'Done'
```

```Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something :       use Python!
Length of the string is 12
Enter something : quit
Done```

```   1 #!/usr/bin/python
2
3 while True:
4     s = raw_input('Enter something : ')
5     if s == 'quit':
6         break
7     if len(s) < 3:
8         continue
9     print 'Input is of sufficient length'
10     # Do other kinds of processing here...
```

```Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit```

end

Python语言的控制流 (last edited 2008-02-23 15:34:06 by localhost)

ch3n2k.com | Copyright (c) 2008 czk. 浙ICP备06000584号