Python's if conditional statement

1.if usage example:

if written statement:
if expression:
    xxxx

(1) the condition is true true (non-empty amount (string, tuple, list, set, dictonary), all non-zero numbers):

    if 1:    
        print 'hello world!'

        print 'True'

    if 'aaa':    
        print 'hello world!'

        print 'True'

(2) condition (the amount of 0, None, empty) is false faulse:

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) other conditions and composition (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, for example:

if else writing:

else statement:
if expression:

    statement(s)
else:

    statement(s)
if 1 < 2:    
    print 'hello world'
else:   
    print 'Oh,no,fourse!'
    print 'main'

3.if elif else wording:

elfi statement:

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'


Guess you like

Origin blog.51cto.com/fengyunshan911/2414549