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: <class 'str'> In line 1 we have created three variables knight, bishop and pawn using unpacking notation. We see that we have assigned strings to them. It is a deliberately broken string, but we know that Python concatenates and treats it as a single string. From there we see that knight stores 'n', bishop, 'b' and pawn, ' '.
In English algebraic chess notation, the N represents a knight, while the B represents a bishop. Pawns have no associated letter. Hence we have put a blank space.
Line 2 makes a selection. Variable piece will have the value of pawn, since it is truthy. Even though it is a blank space, it is still considered truthy. You didn't think it wasn't, did you? Or that it was compressed as an empty string? Neither is the case. In any case, if it had been None, it would not have been selected, because the or operator looks for truthy values.
Then line 3 does something more curious. It assigns to type the type of piece, which is string. Why doesn't this generate an error? Because type is not a reserved word, but a semi-reserved or soft reserved word. It can be reassigned as it happens here, but this is not recommended, of course. Something similar happens with sum and other built-in functions and names.
At the end it is printed <class 'str'>, which is what type is assigned, without generating exceptions.
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: <class 'str'>. En la l铆nea 1 hemos creado tres variables knight, bishop y pawn usando notaci贸n de desempaquetado. Vemos que les hemos asignado cadenas. Es una cadena deliberadamente rota, pero sabemos que Python la concatena y la trata como una sola cadena. A partir de ah铆 vemos que knight almacena 'n', bishop, 'b' y pawn, ' '.
En la notaci贸n algebraica de ajedrez inglesa, la N representa un knight (caballo), mientras que la B representa un bishop (alfil). Los peones no tienen letra asociada. Por eso hemos puesto un espacio en blanco.
La l铆nea 2 hace una selecci贸n. La variable piece tendr谩 el valor de pawn, ya que es veraz. Aunque sea un espacio en blanco, todav铆a se considera veraz. No pensaste que no lo era, 驴verdad? 驴O que estaba comprimido como una cadena vac铆a? Tampoco es el caso. En cualquier caso, si hubiera sido None, no se habr铆a seleccionado, porque el operador o busca valores veraces.
Luego la l铆nea 3 hace algo m谩s curioso. Asigna a type el tipo de piece, que es string. 驴Por qu茅 esto no genera un error? Porque type no es una palabra reservada, sino una semi-reservada o una palabra reservada suave. Se puede reasignar como ocurre aqu铆, pero esto no es recomendable, por supuesto. Algo similar sucede con sum y otras funciones y nombres integrados.
Al final se imprime <class 'str'>, que es lo que se asigna type, sin generar excepciones.
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.