Oppgave
...
Code Block | ||
---|---|---|
| ||
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
Code Block | ||
---|---|---|
| ||
#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
Code Block | ||
---|---|---|
| ||
for rett in kokebok:
print rett + ' inneholder ' + str(kokebok.get(rett)) |
Oppgave 4 - Endre verdier i en dictionary
Code Block | ||
---|---|---|
| ||
for rett in kokebok:
kokebok[rett].append('salt')
kokebok[rett].append('pepper') |
Oppgave 5 - Nyttige dictionaries metoder
Code Block | ||
---|---|---|
| ||
print(kokebok.keys())
print(kokebok.values())
kokebok.clear() |
Oppgave 6 - Enkle sett-operasjoner
Code Block | ||
---|---|---|
| ||
#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
Code Block | ||
---|---|---|
| ||
#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)) |
Oppgave 8 - Fakultet
Code Block | ||
---|---|---|
| ||
def fakultet(n): if n == 0: return 1 else: return n * fakultet(n - 1) |
...