Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Introduction to Lists | Lists
Python Ninja
course content

Contenu du cours

Python Ninja

Python Ninja

1. Contrôles de Base
2. Contrôles Avancés
3. Fonctions
4. Boucles
5. If-Else Statements
6. Défis
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
Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 7. Chapitre 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
Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 7. Chapitre 1
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt