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
course content

Course Content

Python Ninja

Python Ninja

1. Basic Controls
2. Advanced Controls
3. Functions
4. Loops
5. If-Else Statements
6. Challenges
7. Lists

book
Introduction to Lists

Python lists are like containers that can hold different items, such as numbers, words, or objects. You can create a list by enclosing items in square brackets [] and easily add new items to a list using append(), which puts them at the end.

123456789
# Create a list with initial items inventory = ["cat", "monkey"] # Add a new item to the end of the list inventory.append("dog") # Print the updated list print("Updated Inventory:", inventory)
copy

Also you can remove items from the list by using pop(index). This method removes and returns the element at the specified position. If no index is specified, it removes the last item.

1234567891011121314
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Get the values last_item = inventory.pop() second_item = inventory.pop(1); # Print the accessed items print("Last item:", last_item) print("Second item:", second_item) # Print the updated list print("Updated Inventory:", inventory)
copy

In the same way, the ninja's inventory is implemented and can hold values like cat, dog, chicken, monkey, parrot, pig, etc.

You can control it 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 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.

Remember that indexing in a list starts from 0, which means the index of the first element is 0, the second is 1, the third is 2, and so on.

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

py

ninja.py

copy
Task

Swipe to start coding

Solution

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Β 7. ChapterΒ 1
py

ninja.py

toggle bottom row

book
Introduction to Lists

Python lists are like containers that can hold different items, such as numbers, words, or objects. You can create a list by enclosing items in square brackets [] and easily add new items to a list using append(), which puts them at the end.

123456789
# Create a list with initial items inventory = ["cat", "monkey"] # Add a new item to the end of the list inventory.append("dog") # Print the updated list print("Updated Inventory:", inventory)
copy

Also you can remove items from the list by using pop(index). This method removes and returns the element at the specified position. If no index is specified, it removes the last item.

1234567891011121314
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Get the values last_item = inventory.pop() second_item = inventory.pop(1); # Print the accessed items print("Last item:", last_item) print("Second item:", second_item) # Print the updated list print("Updated Inventory:", inventory)
copy

In the same way, the ninja's inventory is implemented and can hold values like cat, dog, chicken, monkey, parrot, pig, etc.

You can control it 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 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.

Remember that indexing in a list starts from 0, which means the index of the first element is 0, the second is 1, the third is 2, and so on.

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

py

ninja.py

copy
Task

Swipe to start coding

Solution

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Β 7. ChapterΒ 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt