python 学习笔记(03)

python 的 if, while, for, break, continue 语句


python 中的语句与其它编程语言格式略有不同

if 语句:

    语法:if a = b :

         print " %s = %s" %(a,b)

       elif a < b :

        print " %s < %s " %(a,b)

       elif a > b :

        print " %s > %s " %(a,b)

例:

#!/usr/bin/env python

a = 10

b = int(raw_input("Enter A by number b:")) #此处是手动输入,相当于read -p

if a == b :

    print "%s = %s " %(a,b)

elif a > b :

    print "%s > %s" %(a,b)

elif a < b :

    print "%s > %s" %(a,b)

else:

    print "erhuo,qingshuru shuzi!!!"


while 语句:

    语法:

    while 条件:

    

    else:


例:

#!/usr/bin/env python

b = 10

running = True


while running:

    a = int(raw_input('Enter A Number b :'))

    if a == b

        print 'good,a is %s equal b is %s' $(a,b)

        running = False

    elif a < b:

        print 'No,it is little b'

    else:

        print 'No,it is grant b'

else:

    print 'The while loop exec end !'


for 循环语句:

    语法:

    for 变量 in 条件或命令:

    

    else:

例:

wKiom1YwpyWCHZLBAAIO8YTRYlo047.jpg


break 语句:

    break 是终止语句的意思,满足条件执行退出操作,具体如下:

wKioL1YwqMmyC6sgAAH5XND0hyI174.jpg

执行结果如下:

wKiom1YwqPCwCvZqAAK1Een6pYU520.jpg


continue 语句:

    满足条件继续进行

例:

wKioL1YwqxPwBoX8AAJU1fx-7yc318.jpg

执行结果如下:

wKiom1Ywqt_yLaSSAAPbDzQU5xc695.jpg


你可能感兴趣的:(python学习)