...

Need some help understanding.

donkon
07-21-2011, 05:32 PM
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:


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.

BluePanther
07-22-2011, 11:49 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum