ackermanmoriii / python-fundamentals

0 stars 0 forks source link

dictionaries 7yo #18

Open ackermanmoriii opened 2 months ago

ackermanmoriii commented 2 months ago

Absolutely, let's dive into Python dictionaries with some fun examples!

Dictionaries are like a special kind of toy chest where each toy has a name tag. You can find your toy by looking at its name tag instead of searching through the whole chest.

Here's how you can create a dictionary of your favorite toys with their names:

favorite_toys = {'teddy bear': 'soft', 'robot': 'metallic', 'puzzle': 'wooden', 'doll': 'plastic'}

In this dictionary, favorite_toys, each toy has a name (like 'teddy bear') and a description (like 'soft').

Accessing Items: To find out what your teddy bear is made of, you can look at its name tag:

teddy_bear_material = favorite_toys['teddy bear']
print(teddy_bear_material)

This will print out soft, because that's the description for your teddy bear.

Changing Items: If you want to change the material of your teddy bear to something else, you can do that too!

favorite_toys['teddy bear'] = 'cotton'
print(favorite_toys)

Now, when you print favorite_toys, it will show {'teddy bear': 'cotton', 'robot': 'metallic', 'puzzle': 'wooden', 'doll': 'plastic'}. Your teddy bear's material is now cotton!

Adding Items: If you get a new toy and want to add it to your dictionary, you can do that!

favorite_toys['car'] = 'plastic'
print(favorite_toys)

Now, when you print favorite_toys, it will show {'teddy bear': 'cotton', 'robot': 'metallic', 'puzzle': 'wooden', 'doll': 'plastic', 'car': 'plastic'}. You've added a car to your dictionary!

Removing Items: If you decide to give away one of your toys, you can remove it from your dictionary.

del favorite_toys['doll']
print(favorite_toys)

Now, when you print favorite_toys, it will show {'teddy bear': 'cotton', 'robot': 'metallic', 'puzzle': 'wooden', 'car': 'plastic'}. The doll is no longer in your dictionary!

Looping Through Dictionaries: You can look at each toy and its description in your dictionary one by one using a loop.

for toy, material in favorite_toys.items():
    print(toy + " is made of " + material)

This will print out each toy and its material on a new line.

Copying Dictionaries: If you want to make an exact copy of your favorite toys dictionary, you can do that too!

all_my_toys = favorite_toys.copy()
print(all_my_toys)

Now all_my_toys is an exact copy of favorite_toys.

Nested Dictionaries: You can also have a dictionary inside another dictionary. It's like having a big toy chest with smaller boxes inside it.

nested_favorite_toys = {
    'soft toys': {'teddy bear': 'cotton'},
    'hard toys': {'robot': 'metallic', 'puzzle': 'wooden'}
}

In this nested dictionary, nested_favorite_toys, we have two categories of toys: soft and hard. Each category has its own box with toys inside.

Remember, dictionaries are like a special kind of toy chest where each toy has a name tag. You can find your toy by looking at its name tag instead of searching through the whole chest! πŸ§ΈπŸ€–πŸ§©πŸŽŽπŸš—πŸ€πŸͺ

ackermanmoriii commented 2 months ago

in this dictionary I want to access 'teddy bear' value, how can i do this ?

To access the value of 'teddy bear' in your nested dictionary, you can follow these steps:

  1. First, you access the 'soft toys' category by using its key:

    soft_toys = nested_favorite_toys['soft toys']
  2. Then, you access the 'teddy bear' inside the 'soft toys' category by using its key:

    teddy_bear_material = soft_toys['teddy bear']
  3. Finally, you print out the material of the teddy bear:

    print(teddy_bear_material)

When you run this code, it will print out cotton, which is the value associated with 'teddy bear' in your nested dictionary. It's like finding a specific toy in a box within a bigger toy chest! πŸ§ΈπŸ“¦