More about FOR loops


#1

Hi guys :slight_smile:

I'm so happy to be here with you. Codecademy gives me so much pleasure and is what I was looking for. I understand most things but I would like to ask for more explanation about dictionaries and FOR loops.

dictionaries are built from two things: ITEM and KEY. Item is the first part, and key is second
example: "Tom [ITEM]" : "Brandt [KEY]". Do I understand it correctly? So instead of printing TOM i can print ITEM.

Now what I don't understand is how mu code works


user_age = {
    'Adam' : 32,
    'Julia' : 23,
    'Tom' : 19,
    'Jackie' : 42,
    'Ted' : 10
    }

user_surname = {
    'Adam' : 'Jordan',
    'Julia' : 'Greth',
    'Tom' : 'Whidaw',
    'Jackie' : 'Chain',
    'Ted' : 'Bundy'
    }

user_email = {
    'Adam' : 'adam@white.com',
    'Julia' : 'julia@watford.com',
    'Tom' : 'tom_w@identify.com',
    'Jackie' : 'j.chain@xos.com',
    'Ted' : 'ted.bundy@murderrer.com'
    }

for item in user_age:
    print (item)
    print ("Name: %s" % item)
    print ("Surname: %s" % user_surname[item])
    print ("Age: %s" % user_age[item])
    print ("Email: %s" % user_email[item])


Ant the output is:

Name: Adam
Surname: Jordan
Age: 32
Email: adam@white.com

I would like to know how my FOR loop workss. As you see my ITEM is the same for every dictionary. If I will print ITEM it will give me ADAM. If I will print the code below it will give me his surname and my question is WHY?

print ("Surname: %s" % user_surname[item])

I don't understand that part. I am referring to ITEM and it gives me "KEY". Would you be so kind and help me to understand.


#2

Hi @cumpelnastodwa,

It's good to hear that you like the codecademy enviroment. :grin:

Please can you rephrase your question. :slight_smile:


#3

if i run your code, i get this output:

Adam
Name: Adam
Surname: Jordan
Age: 32
Email: adam@white.com
Julia
Name: Julia
Surname: Greth
Age: 23
Email: julia@watford.com
Tom
Name: Tom
Surname: Whidaw
Age: 19
Email: tom_w@identify.com
Jackie
Name: Jackie
Surname: Chain
Age: 42
Email: j.chain@xos.com
Ted
Name: Ted
Surname: Bundy
Age: 10
Email: ted.bundy@murderrer.com

item will in turn contain each key of user_age in turn, given you have the same key names for all dictionary's, you can use item to get values of all dictionary's


#4

Hi. I will try and explain my problem.

for item in user_age:
print (item)
print ("Name: %s" % item)
print ("Surname: %s" % user_surname[item])
print ("Age: %s" % user_age[item])
print ("Email: %s" % user_email[item])

I don't understand why user_surname[item] will print out a VALUE instead of an ITEM. My logic is saying it should be user_surname[key] instead of user_surname[item].

EXAMPLE

user = {'Adam' : 'Black'}

for item in user
print item

It will print out Adam in to the console. So why user[item] will print out BLACK in to the console instead of Adam if Adam is the item like in that example from my first post.

Sorry for trouble :slight_smile: I just started with python. Cheers


#5

because item contains 'adam'? Which is the key value


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.