Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: ['pin', 'fork', 'skewer', []] We start by creating two lists tactics1 and tactics2 containing strings with the names of famous chess tactical themes.
Then, line 3 creates a new list t by importing the contents of tactics1 using the asterisk operator (*) and a reference to the whole list tactics2. Both operations are different.
To add more confusion or curiosity, at the end of that line is appended [:], which slices the contents of the newly created list from end to end and this is what is assigned to t. What is so special about this? It is to create a copy, but we will see its real effect later.
Line 4 applies the del operator to delete the entire contents of tactics1 and tactics2. This happens because we have used the slice notation again ([:]). If we had not done this, del would have deleted both identifiers, making their use impossible from that point in the code. Here instead, we just remove the contents of the lists, preserving the identifiers.
Finally, the last line prints the contents of t, which is ['pin', 'fork', 'skewer', []]. This is because the items 'pin', 'fork', 'skewer' were copied as new items from tactics1 in line 3, but the list [] is empty because tactics2 was just referenced. Even though we included a slice operator at the end ([:]), this does not create nested or deep copies; it made no difference. That explains why the list is empty, because it was deleted by del, since we always had the same list referenced everywhere in the script.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: ['pin', 'fork', 'skewer', []]. Comenzamos creando dos listas tactics1 y tactics2 que contienen cadenas con los nombres de temas t谩cticos de ajedrez famosos.
Luego, la l铆nea 3 crea una nueva lista t importando el contenido de tactics1 usando el operador de asterisco (*) y una referencia a toda la lista tactics2. Ambas operaciones son diferentes.
Para agregar m谩s confusi贸n o curiosidad, al final de esa l铆nea se agrega [:], que rebana el contenido de la lista reci茅n creada de un extremo a otro y esto es lo que se asigna a t. 驴Qu茅 tiene de especial esto? Es para crear una copia, pero veremos su efecto real m谩s adelante.
La l铆nea 4 aplica el operador del para eliminar todo el contenido de tactics1 y tactics2. Esto sucede porque hemos usado la notaci贸n de rebanada nuevamente ([:]). Si no hubi茅ramos hecho esto, del habr铆a eliminado ambos identificadores, imposibilitando su uso a partir de ese punto del c贸digo. Aqu铆, en cambio, simplemente eliminamos el contenido de las listas, preservando los identificadores.
Finalmente, la 煤ltima l铆nea imprime el contenido de t, que es ['pin', 'fork', 'skewer', []]. Esto se debe a que los elementos 'pin', 'fork', 'skewer' se copiaron como elementos nuevos de tactics1 en la l铆nea 3, pero la lista [] est谩 vac铆o porque solo se hizo referencia a tactics2. Aunque incluimos un operador de rebanada al final de esa l铆nea ([:]), esto no crea copias anidadas o profundas; no hizo ninguna diferencia. Eso explica por qu茅 la lista est谩 vac铆a, porque fue eliminada por del, ya que siempre tuvimos referencia a la misma lista en todas partes del script.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.