新聞詳情
python教程——字典 in 操作符發(fā)表時(shí)間:2023-02-25 10:37 描述 Python 字典 in 操作符用于判斷鍵是否存在于字典中,如果鍵在字典 dict 里返回 true,否則返回 false。 而 not in 操作符剛好相反,如果鍵在字典 dict 里返回 false,否則返回 true。 語(yǔ)法 in 操作符語(yǔ)法: key in dict 參數(shù) key -- 要在字典中查找的鍵。 返回值 如果鍵在字典里返回true,否則返回false。 實(shí)例 以下實(shí)例展示了 in 操作符在字典中的使用方法: 實(shí)例(Python 3.0+) #!/usr/bin/python3 thisdict = {'Name': 'Runoob', 'Age': 7} # 檢測(cè)鍵 Age 是否存在 if 'Age' in thisdict: print("鍵 Age 存在") else : print("鍵 Age 不存在") # 檢測(cè)鍵 Sex 是否存在 if 'Sex' in thisdict: print("鍵 Sex 存在") else : print("鍵 Sex 不存在") # not in # 檢測(cè)鍵 Age 是否存在 if 'Age' not in thisdict: print("鍵 Age 不存在") else : print("鍵 Age 存在") 以上實(shí)例輸出結(jié)果為: 鍵 Age 存在 鍵 Sex 不存在 鍵 Age 存在 上一篇范彪
文章分類:
信息技術(shù)-教學(xué)資源
|