代码改变世界

第五章 if语句

2017-03-05 14:07  szn好色仙人  阅读(113)  评论(0编辑  收藏  举报
#1.
#A:if语句
Test = [1, 2, 3, 4, 5, 6]
for value in Test:                          #注意:不能少
    if 0 == value % 2 and 0 == value % 3:   #注意:不能少
        print("Type1", end = " ")
    elif 0 == value % 2 or 2 == value % 3:  #注意:不能少 这里是 elif 而不是 else if
        print('Type2', end = " ")
    else:                                   #注意:不能少
        print("Type3", end = " ")
#Type3 Type2 Type3 Type2 Type2 Type1
print("")

#2.
#A:not in
Test = [2, 3, 4]
Test1 = [1, 2, 3]
for value in Test:
    if value not in Test1:
        print("not in: " + str(value), end = ' ')                 
print("")
#not in: 4

#3.
#A:当变量是字符串类型且长度不为0,变量是列表(元组)类型且元素个数不为0时,充当if条件为True
Test = [1, 2, 3]
if Test:
    print("not Null")       #not Null
Test = (1, 2, 3)
if Test:
    print("not Null")       #not Null
Test = "Str"
if Test:
    print("not Null")       #not Null