PYTHON – LIST

Story: The Magic Toy Box

Once upon a time, in a magical land, there was a little girl named Lily. Lily had a special toy box. But this wasn’t an ordinary toy box; it was a magic toy box! In this box, Lily could keep all her favorite toys and find them easily whenever she wanted to play.

Chapter 1: Meeting the Magic Toy Box

Lily’s toy box could remember all her toys in a special order. She could put in dolls, cars, blocks, and even her favorite teddy bear. Let’s see how she does this with her magic toy box:
Explanation:
  • magic_toy_box is like Lily’s magic box where she can keep all her toys.
  • [“doll”, “car”, “blocks”, “teddy bear”] are the toys inside the box.
  • print(magic_toy_box) shows us all the toys inside the magic box.

Chapter 2: Adding New Toys

One day, Lily got a new toy – a robot! She wanted to add it to her magic toy box. So, she used a special magic spell called ‘append’:
Explanation:
  • magic_toy_box.append(“robot”) adds the new toy, the robot, to Lily’s magic toy box.

Chapter 3: Finding a Toy

Sometimes, Lily wanted to play with a specific toy. She could ask her magic toy box for the toy by its number. Each toy had its own number, starting from zero. Let’s see how Lily finds her teddy bear:
Explanation:
  • magic_toy_box[3] is how Lily finds the teddy bear in the magic toy box. Remember, the first toy is number 0, the second toy is number 1, and so on.

Chapter 4: Removing a Toy

One day, Lily decided to give her blocks to her friend. She used another magic spell called ‘remove’ to take the blocks out of the toy box:
Explanation:
  • magic_toy_box.remove(“blocks”) removes the blocks from Lily’s magic toy box.

Chapter 5: How Many Toys?

Lily was curious to know how many toys she had in her magic toy box. She could ask her magic box using a special word ‘len’:
Explanation:
  • len(magic_toy_box) tells Lily how many toys are in the magic toy box

Chapter 6: Playing with the Toys

Sometimes, Lily wanted to play with all her toys one by one. She could ask her magic toy box to show each toy using a magic loop:
Explanation:
  • for toy in magic_toy_box is like telling the magic box to show each toy one at a time.
  • print(toy) shows the name of each toy in the magic toy box
And that’s the story of Lily and her magic toy box! Just like Lily’s magic toy box, Python lists can keep things in order, add new items, find items, remove items, count items, and show each item one by one.

Summary:

  • A list is like a magic toy box where you can keep your toys.
  • Append a new toy to the box.
  • Indexing helps you find a toy by its number.
  • Remove takes a toy out of the box.
  • Len tells you how many toys are in the box.
  • Loops let you see each toy one by one.
I hope you enjoyed the story of Lily and her magic toy box! Now you can use your own magic toy box with Python lists!
Unlock your child’s coding potential with our engaging, story-driven lessons! Enroll today and contact us for more details to embark on this exciting journey! WhatsApp us at http://wa.me/918825618377

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *