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: ab ['A', 'B', 'C']. Line 1 creates a variable m with a string 'abc'. Line 2 creates an anonymous string based on m, but with uppercase characters: 'ABC'.
With this string, in turn, a list n is created where each element of the list consists of each character of the given string. The content of the n list would be ['A', 'B', 'C']. Python can create lists like this in many ways and here it does it with the * as an unpacking operator (yes, once again, the * has yet another distinctive use in Python).
Line 3 has the rest of the quiz gist: The : operator applied to a list or string in this way is used to do slicing. First the string m is sliced from the beginning to exclusive position 2, i.e. only indices 0 and 1 will be extracted, resulting in the value 'ab'.
Then the n list is sliced from the beginning to exclusive position 4. This list is not that big, since its length is 3, but Python is made to do slicing safely, that is, it will only slice up to the actual limit of the list, even if a larger value is specified for it. This will prevent overflow or similar errors. Therefore, what is printed in line 3 is ab ['A', 'B', 'C'].
How did you like it? What was the essence of the quiz for you? Probably the unpacking operator and the slicing operator. But this is not all that these operators can offer. @splash-of-angs63 solved this quiz. Great!
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.
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: ab ['A', 'B', 'C']. La l铆nea 1 crea una variable m con una cadena de caracteres 'abc'. La l铆nea 2 crea una cadena an贸nima basada en m, pero con caracteres en may煤sculas: 'ABC'.
Con esta cadena, a su vez, se crea una lista n donde cada elemento de la lista est谩 formado por cada caracter de la cadena dada. El contenido de la lista n ser铆a ['A', 'B', 'C']. Python puede crear listas como estas de muchas maneras y aqu铆 lo hace con el * como un operador de desempacado (s铆, una vez m谩s, el * tiene otro uso distintivo m谩s en Python).
La l铆nea 3 posee el resto de la esencia del quiz: El operador : aplicado a una lista o cadena de esta manera sirve para hacer rebanado. Primero se rebana la cadena m desde el principio hasta la posici贸n 2 no exclusiva, es decir, que solo se extraer谩n los 铆ndices 0 y 1, lo que se traduce en el valor 'ab'.
Luego se rebana la lista n desde el principio hasta la posici贸n 4 no exclusiva. Esta lista no es tan grande, pues su longitud es de 3, pero Python est谩 hecho para hacer el rebanado de manera segura, es decir, que solo rebanar谩 hasta el l铆mite real de la lista, a煤n cuando se le haya especificado un valor mayor. Esto prevendr谩 errores de desbordamiento o similares. Por tanto, lo impreso en la l铆nea 3 es ab ['A', 'B', 'C'].
驴Qu茅 te ha parecido? 驴Cu谩l fue la esencia del quiz para ti? Probablemente el operador de desempaquetado y el operador de rebanado. Pero esto no es todo lo que estos operadores pueden ofrecer. @splash-of-angs63 resolvi贸 este quiz. 隆Qu茅 bien!
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.