Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2011, 05:32 PM   PM User | #1
donkon
New to the CF scene

 
Join Date: Feb 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
donkon is an unknown quantity at this point
Smile Need some help understanding.

I just started learning python 3 a week ago and have come by this snippet in my learning and cannot understand where the 'K' object comes from as it hasn't been defined. In the tutorial it doesn't explain this. I know this might seem like a dumb question but i really would like to know what's going on:

Code:
def main():
    d = dict(
             one = 1, two = 2, three = 3, four = 4, five = 5
    )
    d['seven'] = 7
    for k in sorted(d.keys()):
        print(k, d[k])
    

if __name__ == "__main__": main()
Hope someone can explain it simply, ty.
donkon is offline   Reply With Quote
Old 07-22-2011, 11:49 AM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
k is being defined in the for loop. for k in d.keys() means k is assigned to the value of d.keys() for every single key in the d dictionary. Output of that should be something like "one 1 two 2.." etc.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.