Returning a dictionary

First, I am new to Python and using the Python Crash Course by Eric Matthes to learn.

I am somewhat puzzled with the code below. I think I understand that the function build_person() takes in the first and last name and puts them into a dictionary and then the value is returned to the call statement which is assigned to the variable musician.

type or paste code here
def build_person(first_name, last_name):
    '''Return a dictionary of information about a person'''
    person = {'first': first_name, 'last':last_name}
    return person

musician = build_person('jimi', 'hendrix')
print(musician)

This is the result, so I can see the keys and values.

{'first': 'jimi', 'last': 'hendrix'}


What I am missing here is how does the function (what appears to be similar to an append) populate the dictionary, and if the data is in the dictionary why can't I also add the below code after print(musician) and see the keys?   I get the error (name 'person is not defined')  which leads me to believe that the dictionary is not defined?????

for name in person.keys():
     print(name.title())

It’s this line that makes the dict.

The {} makes a dictionary. Inside the {} you can add key value pairs.
Like this {“key”: “value”} where the : separates the key and value.

The name ‘person’ is local to the function. You returned the dict in a variable ‘musician’. So you should use

for name in musician.keys():
    print(name.title())

Thank you…Perfect!

Thanks, but what I was struggling to understand was how the arguments were also passed as values to the person dictionary.

After staring at the code for a long time it dawned on me that when the arguments are past to the function, they are also the same values in the person dictionary. Don’t know why it took me so long to figure that out.

def build_person(**first_name**, **last_name**):
    '''Return a dictionary of information about a person'''
    person = {'first': **first_name**, 'last':**last_name**}
    return person

Again, thanks…

When you call the function build_person you provided two strings, ‘jimi’ and ‘hendrix’.

Python arranged that ‘jimi’ could be referred to as first_name and ‘hendrix’ as last_name inside the functions code.

Does that help?

Perfect…Thanks