Last active
November 27, 2018 17:58
-
-
Save guionardo/4f9f94d8360c8a53ddf2389ea1bb2548 to your computer and use it in GitHub Desktop.
Lambda operator example in Python
This file contains 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
#!/bin/python | |
n1=int(input("digite numero :")) | |
n2=int(input("digite numero :")) | |
oper=input("digite soma = +,subtração = -,multiplicação = *,divisao = /:") | |
#soma | |
if (oper=="+"): | |
print("resultado = ",n1+n2) | |
#subtração | |
if (oper=="-"): | |
print("resultado = ",n1-n2) | |
#multiplicação | |
if (oper=="*"): | |
print("resultado = ",n1*n2) | |
#divisão | |
if (oper=="/"): | |
print("resultado = ",n1/n2) | |
# Código otimizado, para tratamento da operação | |
ops = { | |
"+": lambda n1, n2: n1 + n2, | |
"*": lambda n1, n2: n1 * n2, | |
"-": lambda n1, n2: n1 - n2, | |
"/": lambda n1, n2: n1 / n2, | |
} | |
ops["*"](3, 5) # retorna 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment