D
D
Daniiar Kubatbekov2021-12-25 17:41:53
Python
Daniiar Kubatbekov, 2021-12-25 17:41:53

How is the value retrieved from the dictionary?

Hello, I'm learning Python by book and got to the paragraph about a dictionary and iterating over it.
The author provides the following code:

favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
friends = ['phil', 'sarah']
for name in favorite_languages.keys():
    print(name.title())

    if name in friends:
        language = favorite_languages[name].title()
        print(f"\t{name.title()}, I see you love {language}!")

Hence this output:
Jen
Sarah
  Sarah, I see you love C!
Edward
Phil
  Phil, I see you love Python!


I can't understand this line: language = favorite_languages[name].title()
Why does the author write 'name' in square brackets? What does 'name' have to do with dictionary values? And how does this line store the value of the dictionary in the variable, and not the key?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SKEPTIC, 2021-12-25
@ montreal1l

Previously there was a line This line runs through all the keys in the dictionary and writes each of them to the name variable. String Means that the value of the key stored in the variable will be entered into the variable . And so it would be desirable for you to familiarize yourself with the basics of the Python language
for name in favorite_languages.keys():
language = favorite_languages[name].title()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question