Datacamp-python-Dictionaries

1.

Motivation for dictionaries--list中index()的应用,即返回索引,zero-based

# 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])


output:
    berlin


2.

Access dictionary

# 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'])


3.

你可能感兴趣的:(python)