Created
January 6, 2023 18:12
-
-
Save avirup171/bd935271faf31634ecc2507d20e0a340 to your computer and use it in GitHub Desktop.
Python dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Sample dictionary | |
carDetails = { | |
"car_brand":"TATA", | |
"model":"Nexon", | |
"details":"XZA", | |
"fuel":"Petrol" | |
} | |
## Access | |
print(carDetails) | |
print(type(carDetails)) | |
## Access each field | |
print("Name: \t {}".format(carDetails["model"])) | |
## Add new elements | |
carDetails["owner"]="Avirup Basu" | |
print(carDetails) | |
## nested dictionary | |
ownerDetails={ | |
"name":"Avirup Basu", | |
"email":"avirup.basu@live.com" | |
} | |
carDetails["ownerDetals"]=ownerDetails | |
print(carDetails) | |
## Methods | |
#clear | |
carDetails.clear() | |
print(carDetails) | |
#copy | |
copiedCarDetails=carDetails.copy() | |
carDetails.clear | |
print(copiedCarDetails) | |
#get | |
car_brand = carDetails.get("car_brand") | |
print(car_brand) | |
#Items list | |
listItems= carDetails.items() | |
print(listItems) | |
#Items keys | |
listKeys= carDetails.keys() | |
print(listKeys) | |
#Update keys | |
newDictionary = { | |
"kms":"40000", | |
"seat":"5" | |
} | |
carDetails.update(newDictionary) | |
print(carDetails) | |
#Items values | |
print(carDetails.values()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment