thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
# welcome to softhunt.net
# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net', 2: 'By Ranjeet', 'user': 'Greetings to you'}
print("Dictionary", Dictionary)
# accessing a element using key
print("Accessing a element using key:", Dictionary['user'])
# accessing a element using key
print("Accessing a element using key:", Dictionary[2])
# get vs [] for retrieving elements
my_dict = {'name': 'Jack', 'age': 26}
# Output: Jack
print(my_dict['name'])
# Output: 26
print(my_dict.get('age'))
# Trying to access keys which doesn't exist throws error
# Output None
print(my_dict.get('address'))
# KeyError
print(my_dict['address'])
>>> mydict["Apple"]
{'American': '16', 'Mexican': 10, 'Chinese': 5}
IN PYTHON
unlike javascript don't use . operator to create/assign dic. keys to value
use dic[key] syntax not dic.key(like java script)
NOTE:
in pythyon oops we only use . operator to access methods and attributes
and to even access using reference variable like head in linkedlist