首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python – Check if ke

Python – Check if ke

作者头像
py3study
发布于 2020-01-09 11:26:55
发布于 2020-01-09 11:26:55
8330
举报
文章被收录于专栏:python3python3

In Python, you can use the in operator to check if a key exists in a dictionary.

test.py

代码语言:javascript
复制
def main():
    fruits = {
        'apple':1,
        'orange':2,
        'banana':3
    }

    #if key 'apple' exists in fruits?
    if 'apple' in fruits:
        print(fruits['apple'])

if __name__ == '__main__':
    main()

Output

代码语言:javascript
复制
1

P.S Tested with Python 3.4.3 Note has_key() is deprecated in favor of key in d.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/30 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • test.py
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档