-
Created by Unknown User (emilieke), last modified on 25.09.2015
You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 2
Next »
Oppgave 1 - Opprette dictionaries
kokebok = {'pasta carbonara':['fløte','pasta','bacon'], 'sopp risotto':['sopp','ris','fløte'], 'pasta bolognese':['pasta','kjøttdeig','tomatsaus']}
Oppgave 2 - Hente, slette og legge til verdier fra en dictionary
#a
print(kokebok.get('pasta carbonara'))
#b
kokebok['kyllingsalat'] = ['kylling','tomat','agurk','salat']
#c
if 'sopp risotto' in kokebok:
print 'sopp risotto finnes i kokeboken'
else:
print 'beklager, opp risotto finnes ikke i kokeboken'
#d
del kokebok['pasta bolognese']
Oppgave 3 - Iterere over elementer i en dictionary
for rett in kokebok:
print rett + ' inneholder ' + str(kokebok.get(rett))
Oppgave 4 - Endre verdier i en dictionary
for rett in kokebok:
kokebok[rett].append('salt')
kokebok[rett].append('pepper')
Oppgave 5 - Nyttige dictionaries metoder
print(kokebok.keys())
print(kokebok.values())
kokebok.clear()
Oppgave 6 - Enkle sett-operasjoner
#a
primtall = set([2,3,5,7])
#b
print(len(primtall))
#c
primtall.add(11)
primtall.add(13)
#d
primtall.discard(min(primtall))
#e
for tall in primtall:
print(tall)
Oppgave 7 - Union og snitt av sett
#a
oddetall = set([1,3,5,7,9,11,13])
#b
print(oddetall.intersection(primtall))
#c
print(oddetall.union(primtall))
#d
print(oddetall.difference(primtall))
#e
print(primtall.issubset(primtall))