6/19/2021

Python Dictionary

 

# Definition of countries and capital

countries = ['spain''france''germany''norway']

capitals = ['madrid''paris''berlin''oslo']

 

# Get index of 'germany': ind_ger

ind_ger = countries.index('germany')

 

# Use ind_ger to print out capital of Germany

print(capitals[ind_ger])

 

 

# Definition of dictionary

europe = {'spain':'madrid''france':'paris''germany':'berlin''norway':'oslo' }

 

# Print out the keys in europe

print(europe.keys())

 

# Print out value that belongs to key 'norway'

print(europe['norway'])

 

list can not be used as key in dictionary

 

# Definition of dictionary

europe = {'spain':'madrid''france':'paris''germany':'bonn',

          'norway':'oslo''italy':'rome''poland':'warsaw',

          'australia':'vienna' }

 

# Update capital of germany

europe['germany'] = 'berlin'

 

# Remove australia

del(europe["australia"])

 

# Print europe

print(europe)

没有评论: