当前位置:编程学习 > 网站相关 >>

「学习笔记——Python」Python流程控制

Table of Contents
1 if 语句
2 for 语句
3 range函数
4 break 和 continue 语句,循环时使用else
5 pass语句
6 定义函数
7 编程风格
1 if 语句
>>> x = int(raw_input("Please input a number: "))
Please input a number: 3
>>> x 
3
>>> if x < 0:
...      x = 0
...      print 'Negative number'
... elif x == 0:
...      print 'Zero'
... elif x == 1:
...      print 'Single'
... else:
...      print 'More'
... 
More
2 for 语句
python 中的 for 语句和 C 或者 Pascal 中的for语句有所不同,在c中,for 语句通常由 判断语句,条件判断,值变化三部分 构成,python 中 for 语句结构是在一个序列上遍历:
>>> # Measure some string
... words = ['cat', 'windows', 'linux']
>>> for w in words:
...     print w, len(w)
... 
cat 3
windows 7
linux 5
3 range函数
如果你想在一个数字序列上迭代,那么range函数可能对你有用,从下面的例子中体会range 的不同表达式的含义:
>>> range (5)
[0, 1, 2, 3, 4]
>>> range (2,5)
[2, 3, 4]
>>> range (5,10,1)
[5, 6, 7, 8, 9]
>>> range (5,10,2)
[5, 7, 9]
>>> range (5,10,3)
[5, 8]
如果想使用下标在一个序列上迭代,可以使用range函数
>>> words
['cat', 'windows', 'linux']
>>> for i in range(len(words)):
...     print i, words[i]
... 
0 cat
1 windows
2 linux
多数情况下,enumerate函数会更方便。我们在后面的章节会介绍。
4 break 和 continue 语句,循环时使用else
else可以用于for循环,当循环自然终止,即没有被break中止时,会执行else下的语句
>>> for n in range (2,10):
...     for x in range (2,n):
...         if n % x == 0:
...            print n, 'equals', x, '*', n/x
...            break
...     else:
...         print n, 'is a prime number'
... 
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
当用于循环时,else更像try语句中的else,没有例外时执行,而不像 if-else 中的else.
continue语句,从C语言中借鉴而来,直接转入循环的下一次迭代。
>>> for num in range(2,10):
...     if num % 2 == 0:
...        print num, 'is an even number'
...        continue
...     print num, 'is not an even number'
... 
2 is an even number
3 is not an even number
4 is an even number
5 is not an even number
6 is an even number
7 is not an even number
8 is an even number
9 is not an even number
5 pass语句
pass 语句什么都不做,用于需要一个语句以符合语法,但又什么都不需要做时, 也可以用于写一个空函数
>>> while True:
...     pass #busy-wait for keyboard interrupt (Ctrl+c)
... 
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
 
>>> class myEmptyClass:
...     pass
... 
 
>>> def initlog(*args):
...     pass # Remember to implement this
... 
 
6 定义函数
我们可以写一个函数,用于生成Fibonacci数列
>>> def fib(n):
...     """ used to print fibonacci sequence which is less than n """
...     a, b = 0, 1
...     while a < n:
...         print a,
...         a, b = b, a+b
... 
>>> fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
def 用于定义函数,后接函数名和参数列表,函数体必须缩进。 函数体的第一行可以添加函数的文档信息,称为docstring,这个约定可以方便今后对函数信息的提取。
>>> print fib.__doc__
 Used to print fibonacci sequence which is less than n 
7 编程风格
PEP 8 提供了多数项目都遵从的编程风格向导
使用4个空格缩进,不使用tab
同一行不要多于79个字符
使用空行分割函数和类,以及同一函数内较大的块
如果可能的话,将注释与相应代码写在同一行
使用docstring
在操作符两边和分号后使用空格,但是括号内就不必了
保持函数名和类名风格的一致性:CamelCase,lower_case_with_underscore
不要使用特殊的编码,ASCII编码在任何情况下都可以工作良好
 
补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,