Last active
September 27, 2018 03:13
-
-
Save macagua/294a11012301074151a70e7d7a98df1d to your computer and use it in GitHub Desktop.
Uso del fuertemente tipado en Python 2.7.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
""" El fuertemente tipado significa que el tipo de valor no | |
cambia repentinamente. Una cadena que contiene solo dígitos | |
no se convierte mágicamente en un número. Cada cambio de tipo | |
requiere una conversión explícita. | |
>>> valor1 = 2 | |
>>> valor2 = "5" | |
>>> total = valor1 + valor2 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unsupported operand type(s) for +: 'int' and 'str' | |
>>> total = valor1 + int(valor2) | |
>>> print "El total es: " + str(total) | |
7 | |
""" | |
valor1 = 2 # "valor1" guarda un valor entero | |
valor2 = "5" # "valor1" guarda un valor cadena | |
total = valor1 + int(valor2) # se usa el metodo int() para convertir a entero | |
print "El total es: " + str(total) # se usa el metodo str() para convertir a cadena | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment