Wednesday, March 13, 2019

Kawaljit Singh asked:

Explain the code along with output

adict={'Bhavna':1,'Richad':2,'Firoza':10,'Arshnoor':20}
temp = ""
for key in adict:
    if temp<key:
        temp=key
print(temp)


To understand this in a better way, please write the following code and inspect what happens to the variables in each pass of the for loop.

adict={'Bhavna':1,'Richad':2,'Firoza':10,'Arshnoor':20}
temp = ""
for key in adict:
    print("key =", key, "and temp =", temp)
    print("so", temp, "<", key, "is", temp<key)
    if temp<key:
        temp=key
        print("so temp =", key)
    print("--------next pass--------")
print(temp)


Hope this helps.

No comments: