Sunday, 24 February 2019

python dictionary

customer = {
    'name': 'sam',
    'age': 30,
    'employed': True
}

print(customer['name'])
print(customer.get('birthdate', 'Jan 1 1980'))

print(customer['birthdate'])

customer['name'] = 'Jack'
print(customer['name'])

customer['birthdate'] = 'Dec 31, 1979'
print(customer['birthdate'])

sam
Jan 1 1980
Jack

Traceback (most recent call last):
  File "c:\Users\bob\python1\.vscode\tempCodeRunnerFile.py", line 9, in <module>
    print(customer['birthdate'])
KeyError: 'birthdate'

Dec 31, 1979

No comments:

Post a Comment