Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Introduction to Lists | Lists
Python Ninja
SectionΒ 6. ChapterΒ 1
ninja.py

ninja.py

bookIntroduction to Lists

Swipe to show menu

Python lists are like containers that can hold different items. You can create a list by enclosing items in square brackets []:

list_ = ["cat", "dog", "chicken", "monkey"]

In the Ninja Game, you can interact with the list using the following methods:

  • pick_to_inventory(index): picks up an item and places it in the inventory at the specified index, or adds it to the end of the list (or the first element if the list is empty) if no index is provided;
  • put_from_inventory(index): takes an item from the inventory by index and places it on the map. If no index is provided, it takes the last item.

Here is an example where the Ninja collects 2 animals into the inventory and then places the first element back on the map.

Note

In Python, counting starts from 0.

Note

In this course, you do not need to define lists yourself β€” they are already built into the code logic.

ninja.py

ninja.py

copy

When you run the code on the map, you will see a purple bar at the top β€” this is your inventory.

Task

Swipe to start coding

Solution

Explore other courses in Catalog

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 1
ninja.py

ninja.py

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt