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: ([1, 2, 3, 4, 4],). We start by creating a tuple containing the list [1, 2, 3, 4]. As we can see, this tuple a has a comma at the end, and Python connoisseurs know that this is mandatory in order to denote a tuple of at least one item. Otherwise, the parenthesis would not create a tuple and would instead represent a grouping.
This explains why by doing b = a[-1], we are accessing the last element of the tuple, which is the mentioned list and not an empty element. Surely you knew this, it's very basic.
What you might not know are all the roles of all the parameters of all the functions and methods that we use even on a daily basis. This may be the case of the instruction b.insert(5, 4), which highlights the insert method. We know that it should insert something into list b, but how should it be read? Which parameter represents the content to be inserted and which parameter represents the position where it will be inserted? The answer is that the first argument is the position and the second argument is the content.
So, since b has [1, 2, 3, 4], the instruction b.insert(5, 4) will insert a 4 at position 5, so we get [1, 2, 3, 4, 4, 4]. Of course, what is printed at the end is ([1, 2, 3, 4, 4],) because although the wrapper is a tuple and as such is immutable, its content is a list and this is mutable. We accessed it by referencing it with the variable b and then modified it with insert.
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: ([1, 2, 3, 4, 4],). Comenzamos creando una tupla que contiene la lista [1, 2, 3, 4]. Como podemos ver, esta tupla a tiene una coma al final, y los conocedores de Python saben que esto es obligatorio para denotar una tupla de al menos un elemento. De lo contrario, el par茅ntesis no crear铆a una tupla sino que representar铆a una agrupaci贸n.
Esto explica por qu茅 al hacer b = a[-1], estamos accediendo al 煤ltimo elemento de la tupla, que es la lista mencionada y no un elemento vac铆o. Seguro que lo sab铆as, es muy b谩sico.
Lo que quiz谩s no sepas son todas las funcionalidades de todos los par谩metros de todas las funciones y m茅todos que utilizamos incluso a diario. Este puede ser el caso de la instrucci贸n b.insert(5, 4), que destaca el m茅todo insert. Sabemos que deber铆a insertar algo en la lista b, pero 驴c贸mo deber铆a leerse? 驴Qu茅 par谩metro representa el contenido a insertar y qu茅 par谩metro representa la posici贸n donde se insertar谩? La respuesta es que el primer argumento es la posici贸n y el segundo argumento es el contenido.
Entonces, dado que b tiene [1, 2, 3, 4], la instrucci贸n b.insert(5, 4) inserta un 4 en la posici贸n 5, por lo que obtenemos [1, 2, 3, 4, 4, 4]. Por supuesto, lo que se imprime al final es ([1, 2, 3, 4, 4],) porque aunque el contenedor es una tupla y como tal es inmutable, su contenido es una lista y esto es mutable. Accedimos a 茅l haciendo referencia a 茅l con la variable b y luego lo modificamos con insert.
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.