Python 練習14---if...in

in 如果在指定的序列中找到值返回 True,否則返回 False。
not in 如果在指定的序列中沒有找到值返回 True,否則返回 False。可以參看:《Python 運算符》http://blog.csdn.net/u013372487/article/details/51579661

也可以用於字典:字典 in 操作符用於判斷鍵是否存在於字典中,如果鍵在字典dict裏返回true,否則返回false。

練習:

#Hello World program in Python
a_list=[1,2,35,6]
if 3 in a_list:
    print("3 in list")
else:
    print("3 not in list")

a_str="hello morning!"
if "moring" in a_str:
    print("match morining")
else:
    print("morning not match")

a_dict={}
a_dict["3"]="three"
if "3" in a_dict.keys():
    print("dict match 3",a_dict["3"])

運行結果
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章