Contenu du cours
Python Ninja
Python Ninja
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.
# 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)
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.
# 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)
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 specifiedindex
, or adds it to the end if noindex
is provided.put_from_inventory(index)
: Takes an item from the inventory byindex
and places it on the map. If noindex
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.
ninja.py
Swipe to start coding
Solution
Merci pour vos commentaires !
ninja.py
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.
# 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)
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.
# 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)
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 specifiedindex
, or adds it to the end if noindex
is provided.put_from_inventory(index)
: Takes an item from the inventory byindex
and places it on the map. If noindex
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.
ninja.py
Swipe to start coding
Solution
Merci pour vos commentaires !