1. 程式人生 > >python入門學習筆記(四)——if語句

python入門學習筆記(四)——if語句

5. if語句

5.1  簡單if語句

sum = 20;

if sum < 19:

    print('OK')

    

 

5.2  if-else語句

name = ['sss','dddd','eeee']

if name == 'cccc':

    print('OK')

else:

    print('NO')

##########################

NO

5.3 if-elif-elif(多條件)

age = 12

if age < 4 :

    print('年齡小於四歲。')

elif age < 10 :

    print('年齡大於等於四歲,小於十歲。')

elif age > 10 :

    print('年齡大於十歲。')

5.4 if-elif-elif-else(多條件)

age = 12

if age < 4:

    print('年齡小於四歲

')

elif age <10:

    print('年齡小於十歲大於等於四歲')

else:

    print('年齡大於等於十歲')

5.5 andor 類似邏輯運算子 &&||

age = 12

if age < 4 and age < 8:

    print('pa = 0')

 

else:

    print('pa = 1')

######################

pa = 1