How can I add new keys to a Python dictionary?

I am working on a Python project where I need to add new keys to a dictionary. I am not sure how to do it correctly and keep getting an error. Here is my code:

my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict['d'] = 4

I am trying to add a new key-value pair to my_dict using the square bracket notation. Can anyone help me figure out what I’m doing wrong? And is there another way to add new keys to a dictionary in Python?

Also, once I have added a new key to the dictionary, how can I access its value? Is there a specific syntax I need to use?

I would appreciate any help or guidance on this issue, as I am fairly new to Python and still learning. Thank you in advance!