import pywikibot

site = pywikibot.Site('wikidata', 'wikidata') # for testing use parameter ('test', 'wikidata') to edit
repo = site.data_repository()

# this speeds up the bot run to 1 edit per second (default value: one edit per 10 seconds in PAWS); deactivated to be careful in the beginning
timeToWaitBetweenEdits = 1
site.throttle.setDelays(delay=timeToWaitBetweenEdits, writedelay=timeToWaitBetweenEdits, absolute=True)

# input definition
urlbase = '{}' # für Bremen
inputdata = { 'Q52818252':'659' } # a python dictionary; pairs of Q-ID (dict key) and identifiers (dict value), separated by comma

for inKey, inValue in inputdata.items(): # loop: inKey is the Q-ID from inputdata, inValue the identifier
    Qitem = pywikibot.ItemPage(repo, inKey) # pull item object from Q-ID
    fullUrl = urlbase.format(inValue) # create the actual URL
    urlClaim = pywikibot.Claim(repo, 'P973')
    try: # exception handling required to check whether the item actually exists
        Qitem.addClaim(urlClaim, summary='') # adds the claim with a custom edit summary
        print('Added to {}: URL "{}"'.format(inKey, fullUrl))
    except (pywikibot.NoPage, pywikibot.OtherPageSaveError):
        print('item {} does not exist; skip'.format(inKey))
Added to Q52818252: URL ""