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

Contenido del Curso

Ninja de Python

Ninja de Python

1. Controles Básicos
2. Controles Avanzados
3. Funciones
4. Bucles
5. Sentencias If-Else
6. Desafíos
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
Tarea

Swipe to start coding

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 7. Capítulo 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
Tarea

Swipe to start coding

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 7. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Lamentamos que algo salió mal. ¿Qué pasó?
some-alt