Hashmaps, also known as dictionaries, are a powerful data structure in Python that store data in key-value pairs. They are an efficient way to store and retrieve data, as they allow you to access values using unique keys. In this article, we will explore the capabilities of hashmaps in Python, as taught by W3Schools.

Hashmaps have a wide range of applications, from data lookup to stock monitoring. Their performance does not degrade as the number of items increases, making them ideal for large data sets. Now, let's dive into the details of how to use hashmaps in Python.

Creating a Hashmap in Python
A hashmap is defined using curly braces {} in Python. Inside, you can define key-value pairs. Keys must be unique and immutable, while the values can have repeated and mutable content. Here's a simple example:

```python my_hashmap = {"name": "John", "age": 30, "city": "New York"} ```
Accessing Hashmap Values

To access the value associated with a specific key, use the key in square brackets after the hashmap's variable name. For instance, `my_hashmap["name"]` will return 'John'.
```python print(my_hashmap["name"]) # Outputs: John ```
Adding and Removing Items

You can add or remove items in a hashmap with ease. Adding an item is as simple as assigning a key-value pair, e.g., `my_hashmap["job"] = "Engineer"`. To remove an item, use the `del` keyword, like so: `del my_hashmap["age"]`.
Methods and Attributes of Hashmaps
Python hashmaps have several built-in methods and attributes that make them even more versatile. Some of the most useful ones include:

len() Function
The `len()` function returns the number of items in the hashmap. For example, `len(my_hashmap)` would return 3 for the initial hashmap created above.




















get() Method
The `get()` method returns the value for the given key if key is in hashmap, else returns None. It also allows you to provide a default value if the key doesn't exist.
```python print(my_hashmap.get("gender")) # Outputs: None print(my_hashmap.get("gender", "Unknown")) # Outputs: Unknown ```
items() Method
The `items()` method returns a view object that displays a list of a dictionary's key-value tuple pair.
```python for key, value in my_hashmap.items(): print(f"{key}: {value}") ```
Looping Through a Hashmap
You can easily loop through a hashmap using either the `for` loop with the `.items()` method or a comprehension.
For Loop
The `for` loop can be used to iterate over a hashmap's keys, values, or items.
```python for key in my_hashmap: print(key) for value in my_hashmap.values(): print(value) for key, value in my_hashmap.items(): print(f"{key}: {value}") ```
Comprehension
Here's an example of using a comprehension to create a list of key-value pairs:
```python [key_value_pairs] = [(k, v) for k, v in my_hashmap.items()] ```
Given their utility and ease of use, hashmaps are a cornerstone of Python programming. They streamline data retrieval and manipulation, making complex tasks simpler. Now that you've seen a few examples, it's time to explore hashmaps further and apply them in your own projects.