My Coding Quiz #10

Words
568
Reading
3 min
3y
Listen to this post
~4 min

My Coding Quiz #10 👨‍💻🛠️🧩

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.

Without further ado, here's the riddle...




Quiz
By eniolw@eniolw


What's your choice?

Solution to the previous quiz: ['888', '888']. First a variable w of numeric value 4 is created. This value is then doubled and converted to a character, resulting in the value '8' being assigned to the variable x.

Next, on line 3 we get a string by tripling the contents of x, resulting in the immutable value '888'. This is a Python feature that applies arithmetic operators to strings, so this is not a bug. Along the same line, a y list is created with the newly obtained value.

On line 4 we see another Pythonic trick that applies the * operator to lists. What it does is create a new list with as many copies of y as specified. In this case it's two copies of y (z = y * 2), which translates to ['888', '888'] which is the value printed on the final line.

Did you know these other uses of the * operator in Python? Believe me, they are not all there is! Congratulations to splash-of-angs63@splash-of-angs63 for solving this quiz.


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, Hive Learners and others.


Mi Quiz de Programación #10 👨‍💻🛠️🧩

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é.

Sin más preámbulos, he aquí el acertijo...




Quiz
Por eniolw@eniolw


¿Cuál es tu elección?

Solución al quiz anterior: ['888', '888']. Primero se crea una variable w de valor numérico 4. Seguidamente, este valor se duplica y se convierte a un caracter, lo que resulta en el valor '8' que se asigna a la variable x.

Acto seguido, en la línea 3 obtenemos una cadena de caracteres triplicando el contenido de x, dando como resultado el valor inmutable '888'. Se trata de un artificio de Python que aplica operadores aritméticos a las cadenas de caracteres, por lo que no se trata de un error. En esa misma línea, se crea una lista y con el valor recien obtenido.

En la línea 4 volvemos a ver otro artificio Pythónico que aplica el operador * a las listas. Lo que hace es crear una nueva lista con tantas copias de y como se haya especificado. En este caso son dos copias de y (z = y * 2), lo que se traduce en ['888', '888'] que es el valor impreso en la línea final.

¿Conocías estos otros usos del operador * en Python? ¡Créeme, no son todos los que hay! Felicitaciones a splash-of-angs63@splash-of-angs63 por resolver este quiz.


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, Hive Learners y otras.