What I learnt Today
- How to define dictionaries using dict constructor and curly braces {}
- How to access elements in a list usin the .get method() and variable name with brackets[]
- How to add and update elements in dictionary
- How to loop through keys , values and both keys and values using a for loop.
On what I learnt Today
Assumption: We have a dictionary of the name Person.
Syntax:
- Defining: person = { "name" : "James" , "age" : 23 , "Occupation" : "Engineer" }
Accessing Elements = print(person["name"]) OR print(person.get("name"))
Adding Elements in a Dictionary
person["status"] = "Graduated"Updating elements in a Dictionary.
person["age"] = 20Removing an Element from a Dictionary
person.pop("status")Looping through respecive keys
for key in person:
print(key)Looping through respective Values
for value in person.values():
print(value)Looping through respective keys and Values
for key , value in person.items():
print(f"{key} -> {value}")
Resources I used
Python refresher series by Bonaventure Ogeto on Youtube.
What's Next
Understanding sets and Tuples.
Top comments (0)