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: -5.0 <class 'float'>. Lines 1, 2 and 3 describe the annotation of variables a, b, c. Variable annotation (AKA Type hints) consists of associating a data type to a variable. In this case we are associating a with the type float, and b and c with the type int. The emphasis is on the word associate, which does not mean to create or declare and its purpose is mainly to help in the description and documentation of the code.
That means that those variables don't actually possess a type, but that the value they will possess is "expected" to be of that type with which it was associated. As described by @edb in the previous post:
While values have a type, variables don't. They're just references to a value object.
This variable annotation in the code block of the quiz is unnecessary, as it makes more sense to use it when writing function parameters. However, the statements are still valid and serve to test your knowledge, especially in light of the following lines.
So, line 4 is creating a variable a and assigning it the value -2.5 which is intrinsically of type float. Line 5 creates a variable b and references variable a. Line 6 creates a variable c and assigns the value of the sum of the values of a and b to it.
The point is that the types noted in lines 1, 2, and 3 have nothing to do with the following lines; it is as if they did not exist or were comments. Therefore, what is printed in line 7 is -5.0 <class 'float'>, since -5.0 is the sum of -2.5 and -2.5, which are float.
PS: I noticed that the quiz options had a couple of typos. I have fixed them. If you find them, feel free to let me know. On this occasion, @rafaelaquino has come up with the correct answer. Very good!
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: -5.0 <class 'float'>. Las l铆neas 1, 2 y 3 describen la anotaci贸n de las variables a, b, c. La anotaci贸n de variable (tambi茅n conocida como sugerencias de tipo) consiste en asociar un tipo de datos a una variable. En este caso estamos asociando a con el tipo float, y b y c con el tipo int. El 茅nfasis est谩 en la palabra asociar, que no significa crear o declarar y su prop贸sito es principalmente ayudar en la descripci贸n y documentaci贸n del c贸digo.
Eso significa que esas variables en realidad no poseen un tipo, pero que se "espera" que el valor que poseer谩n sea del tipo con el que se asoci贸. Como lo describe @edb en la publicaci贸n anterior:
Mientras que los valores tienen un tipo, las variables no. Son solo referencias a un objeto de valor.
Esta anotaci贸n de variable en el bloque de c贸digo del quiz es innecesaria, ya que tiene m谩s sentido usarla al escribir par谩metros de funciones. Sin embargo, las declaraciones siguen siendo v谩lidas y sirven para probar tus conocimientos, especialmente a la luz de las siguientes l铆neas.
Entonces, la l铆nea 4 crea una variable a y le asigna el valor -2.5 que es intr铆nsecamente de tipo float. La l铆nea 5 crea una variable b y hace referencia a la variable a. La l铆nea 6 crea una variable c y le asigna el valor de la suma de los valores de a y b.
El punto es que los tipos anotados en las l铆neas 1, 2 y 3 no tienen nada que ver con las siguientes l铆neas; es como si no existieran o fueran comentarios. Por lo tanto, lo que se imprime en la l铆nea 7 es -5.0 <class 'float'>, ya que -5.0 es la suma de -2.5 y -2.5, que son flotantes.
PD: not茅 que las opciones del quiz anterior ten铆an un par de errores tipogr谩ficos. Los correg铆. Si los encuentras, no dudes en hac茅rmelo saber. En esta ocasi贸n, @rafaelaquino ha dado con la respuesta correcta. 隆Muy 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.