Python中的流程控制--if判断语句

1.if 用法举例:

    if语句写法:

    if expression:

    statement(s)

    注:python使用缩进作为其语句分组的方法,建议使用4个空格。

(1)条件为真true (非空的量(string,tuple,list ,set,dictonary),所有非零的数):

    if 1:

        print 'hello world!'

        print 'True'

        

    if 'aaa':

        print 'hello world!'

        print 'True'

(2)条件为假 faulse(0,None,空的量):

    

    if  0:

        print 'hello world!'

        print 'True'

   

    if None:

        print 'hello world!'

        print 'True'

   

     if  '':

        print 'hello world!'

        print 'True'

   

     if  1>2:

        print 'hello world!'

        print 'True'

(3)组合条件及其他(and /or ):

    if  not 1>2:

        print 'hello world!'

        print 'True'

    if  not 1>2 and 1 == 1:

        print 'hello world!'

        print 'True'

2.if   else 举例:

if  else写法:

    else语句:

    if expression:

        statement(s)

    else:

        statement(s)


    if 1 < 2:

        print 'hello world'

    else:

        print 'Oh,no,fourse!'

    print 'main'

3.if   elif   else写法:

elfi 语句:

    if expression1:

        statement1(s)

    elif expression2:

        statement2(s)

    else:

        statement3(s)


    if 1 < 2:

        print 'hello world'

    elif 'a':

        print 'aaaaa'

    else:

        print 'Oh,no,fourse!'


4.举例1:

    #!/usr/bin/env python

    score =int( raw_input(‘Please input a num:’))

    if score >= 90:

        print 'A'

        print 'Very good'

    elif score >=80:

        print 'B'

        print 'good'

    elif score >=60:

        print 'C'

        print 'pass'

    else:

        print 'D'

    print 'END'

5.举例2:and or 应用:

多个条件下判断:

转换大小写:

    a.lower()

    a.upper()


    #!/usr/bin/env python

    yn = raw_input("Please input [Yes/No]:")

    yn = yn.lower()

    if yn == 'y' or yn == 'yes':

        print "Programe is running..."

    elif yn == 'n' or yn == 'no':

        print "Programe is exit."

    else:

        print "Error,Please input [Yes/No]"


6.习题

1. 输入三个整数x,y,z,请把这三个数由小到大输出。   1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,   然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

    #!/usr/bin/env python

    x = int(raw_input("Please input First number: "))

    y = int(raw_input("Please input second number: "))

    z = int(raw_input("Please input Three number: "))

    list =(x,y,z)

    x=list[0]

    y=list[1]

    z=list[2]

    if x <  y  and x < z :

        if y < z :

            print "%s<%s<%s" % (x,y,z)

        else:

            print "%s<%s<%s" % (x,z,y)

    elif x < y and x > z:

        print "%s<%s<%s" % (z,x,y)

    elif  x >y and x

        print "%s<%s<%s" % (y,x,z)

    elif x> y  and x > z:

        if y>z:

            print "%s<%s<%s" % (z,y,x)

        else:

            print "%s<%s<%s" % (y,z,x)

    print "END"


2. 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高   于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提   成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于   40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于   100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

#!/usr/binenv python

#

本文转自 枫叶云  51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2054606



你可能感兴趣的:(Python中的流程控制--if判断语句)