Python 之if条件判断语句

1.if 用法举例:

if语句写法:
if 表达式:
    xxxx

(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'


猜你喜欢

转载自blog.51cto.com/fengyunshan911/2414549