import pywikibot
site = pywikibot.Site('test', 'wikidata')
repo = site.data_repository()
ZURICH = u'Q83992'
CONTAINS = u'P395'
HAS_COLOR = u'P42284'
# retrieve the ZURICH item
zh = pywikibot.ItemPage(repo, ZURICH)

# get claims
zh_claims = zh.get()['claims']
{'P395': [< object at 0x7f49012f9780>, < object at 0x7f49012b0438>]}
# iterate over CONTAINS claims...
for contains_claim in zh_claims[CONTAINS]:
    # ... handle trees (all items are trees in our example)
    tree = pywikibot.ItemPage(repo,
    tree_dict = tree.get()

    # print item description
     # get HAS_COLOR claims
    tree_color_claims = tree_dict['claims'][HAS_COLOR]

    # ...iterate over them ...
    for tree_color_claim in tree_color_claims:
        # ... if the target of the HAS_COLOR claim is 'purple'...
        if == u'purple':
            print('found a purple tree')
            # ... create new 'has color' claim with 'green' as value
            green_tree_claim = pywikibot.Claim(repo, HAS_COLOR)
   = u'green'
            # add it to the tree (this is the bot part)
            tree.addClaim(green_tree_claim, \
                          summary=u'add claim that this should be a green tree')
            print('added green as color')
{'en': 'A green tree in Zurich'}
{'en': 'purple colored tree in Zurich'}
found a purple tree
added green as color