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: NameError. Line 1 creates a zip object, which is useful for traversing two iterables in parallel. In this case it would be the string "abc" and the range(1,4) which, if resolved as a list, would consist of [1, 2, 3], so both iterables are the same size.
Line 2 creates a dictionary d using the compression notation. We see that it would consist of key/value pairs formed by each parallel element traversed with the zip object, so if we were to print d, it would consist of {'a': 1, 'b': 2, 'c': 3}.
Line 3 should show one result, whichever is truthy of the three values joined with the or operator. Let's look at each one:
馃憠 list(c) will, in fact, produce the empty list [], since zip is like a generator. This means that once it is traversed, it is consumed. We see that it was traversed on line 2, so it is already consumed at this point and produces nothing. An empty list is falsy.
馃憠 d.get(1) returns None since there is no key named 1 in the d dictionary. The None value is obviously falsy.
馃憠 a, on the other hand, will trigger the NameError, since it is a local variable inside the compressor block. It does not exist outside it. This explains the result obtained, which is not really an output, but an execution error.
What do you say, were you right with your choice?
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: NameError. La l铆nea 1 crea un objeto zip, que es 煤til para atravesar dos iterables en paralelo. En este caso ser铆a la cadena "abc" y el range(1,4) que, si se resuelve como una lista, consistir铆a en [1, 2, 3], por lo que ambos iterables tienen el mismo tama帽o.
La l铆nea 2 crea un diccionario d usando la notaci贸n de compresi贸n. Vemos que consistir铆a en pares clave/valor formados por cada elemento paralelo atravesado con el objeto zip, por lo que si imprimi茅semos d, consistir铆a en {'a': 1, 'b': 2, 'c': 3}.
La l铆nea 3 deber铆a mostrar un resultado, el que sea verdadero (truthy) de los tres valores unidos con el operador or. Veamos cada uno:
馃憠 list(c) producir谩, de hecho, la lista vac铆a [], ya que zip es como un generador. Esto significa que una vez que se atraviesa, se consume. Vemos que fue recorrido por la l铆nea 2, por lo que ya est谩 consumido en este punto y no produce nada. Una lista vac铆a es falsa (falsy).
馃憠 d.get(1) devuelve None ya que no hay ninguna clave denominada 1 en el diccionario d. El valor None es obviamente falso (falsy).
馃憠 a, por otro lado, desencadenar谩 el NameError, ya que es una variable local dentro del bloque compresor. No existe fuera de 茅l. Esto explica el resultado obtenido, que en realidad no es una salida, sino un error de ejecuci贸n.
驴Qu茅 dices, acertaste con tu elecci贸n?
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.