note 4 三大结构
程序流程图


顺序结构
选择结构
if

if-else

if 语句-嵌套结构(Nested)

多分支结构(Chained)
if score >= 90:
    print 'ARM'
elif score >= 80:
    print 'B'
elif score >= 70:
    print 'C'
elif score >= 60:
    print 'D'
if-elif-else语句:
elif相当于else:if
if-elif-else语句中有else条件时,else条件放在最后,否则SyntaxError
一元二次方程

import math
a = float(raw_input('Enter a'))
b = float(raw_input('Enter b'))
c = float(raw_input('Enter c'))
if a != 0:
    delta = b**2 - 4 * a * c
    if delta < 0:
        print 'No solution'
    elif delta == 0:
        s = -b/(2 * a)
        print 's:',s
    else:
        root = math.sqrt(delta)
        s1 = (-b + root) / (2 * a)
        s2 = (-b - root) / (2 * a)
        print 'Two distimct solutions are:',s1,s2



循环结构
while
count = 0
while count < 5:
print 'Programming is fun!'
count += 1
死循环
多次求解一元二次方程
import math
ch = ''
while ch != 'q':
    a = float(raw_input('Enter a'))
    b = float(raw_input('Enter b'))
    c = float(raw_input('Enter c'))
    if a != 0:
        delta = b**2 - 4 * a * c
        if delta < 0:
            print 'No solution'
        elif delta == 0:
            s = -b/(2 * a)
            print 's:',s
        else:
            root = math.sqrt(delta)
            s1 = (-b + root) / (2 * a)
            s2 = (-b - root) / (2 * a)
            print 'Two distimct solutions are:',s1,s2
    ch = raw_input('Quit?')
break语句
结束当前循环体
continue语句
结束当次循环
range函数

求常数e



求常数π


冰雹猜想(序列)

for n in range(1,100):
    while n != 1:
        if n % 2 == 0:
            n /= 2
        else:
            n = 3 * n + 1
            print n
while vs. for 循环
while 循环更通用
任何for循环写到程序都能用while循环实现
适用场景:
for循环:已知循环的范围(range),即起止值和步长
while循环:其他情况,入:不确定循环何时终止
    http://www.cnblogs.com/OceanF/
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号