Friday, March 27, 2020

dictionary in python


elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},
            'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

print(elements)
print("\n hydrogen:",elements['hydrogen'])
print("\n hydrogen_no:",elements['hydrogen']['number'])
print("\n hydrogen_weight:",elements['hydrogen']['weight'])
print("\n hydrogen_symbols:",elements['hydrogen']['symbol'])

print("\n helium:",elements['helium'])

output:
{'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'}, 'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

 hydrogen: {'number': 1, 'weight': 1.00794, 'symbol': 'H'}

 hydrogen_no: 1

 hydrogen_weight: 1.00794

 hydrogen_symbols: H

 helium: {'number': 2, 'weight': 4.002602, 'symbol': 'He'}


example 2:
cast = {
           "Jerry Seinfeld": "Jerry Seinfeld",
           "Julia Louis-Dreyfus": "Elaine Benes",
           "Jason Alexander": "George Costanza",
           "Michael Richards": "Cosmo Kramer"
       }
for key in cast:
    print(key)

output:
Jerry Seinfeld
Julia Louis-Dreyfus
Jason Alexander
Michael Richards

No comments:

Post a Comment

python class topic video