Here is the code to find the sum of all keys in a dictionary in Python using a loop:
main.py162 chars8 lines
Output:
main.py44 chars2 lines
Explanation: We first create a dictionary my_dict
with some key-value pairs. We then initialize a variable key_sum
to 0. We use a for
loop to iterate through all the keys in the dictionary, and for each key, we add it to key_sum
. Finally, we print out the total sum of all keys in the dictionary.
gistlibby LogSnag